-1

I'm trying to figure out how can i send two items (Up and down inside a column) from sql server.

Here is my existing code:

SqlDataAdapter sda = new SqlDataAdapter(" SELECT name,Subname,Code,Price from Customers", con);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        AppointmentGrid.Rows.Clear();
        foreach (DataRow item in dt.Rows)
        {
            int n = AppointmentGrid.Rows.Add();

            AppointmentGrid.Rows[n].Cells[0].Value = item[0].ToString();
            AppointmentGrid.Rows[n].Cells[1].Value = item[1].ToString();
            AppointmentGrid.Rows[n].Cells[2].Value = item[2].ToString();
            AppointmentGrid.Rows[n].Cells[3].Value = item[3].ToString();
          }

I need 'Name' and 'Subname' to be inside one column in my datagridview.

Dss
  • 3
  • 6

1 Answers1

0

I need 'Name' and 'Subname' to be inside one column in my datagridview.

You can use 2 approaches to do that.

First Approach

When you get data from the database, concatenate the 2 columns:

SELECT name + ' ' + Subname as NameAndSubname, Code, Price from Customers

Second Approach

Do the concatenation on the application side:

AppointmentGrid.Rows[n].Cells[0].Value = 
    item[0].ToString() + " " + item[1].ToString();
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • Thank you your code works but what if i want to add new line??This code it adds items side by side – Dss Nov 19 '17 at 21:08
  • @Dss Please see [this](https://stackoverflow.com/questions/16514352/multiple-lines-in-a-datagridview-cell) for how to do that. – CodingYoshi Nov 19 '17 at 21:26
  • Thank you Solved with DeafaultCellStyle Wraped and autosizerowmode -AllCells – Dss Nov 19 '17 at 21:26
  • @Dss Since you are new, please read [this](https://stackoverflow.com/help/someone-answers) when you have time. – CodingYoshi Nov 19 '17 at 22:02