Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DatagridView在一列中显示多个项目_C# - Fatal编程技术网

C# DatagridView在一列中显示多个项目

C# DatagridView在一列中显示多个项目,c#,C#,我试图弄清楚如何从SQLServer发送两个项目(在一列中向上和向下) 以下是我现有的代码: SqlDataAdapter sda = new SqlDataAdapter(" SELECT name,Subname,Code,Price from Customers", con); DataTable dt = new DataTable(); sda.Fill(dt); AppointmentGrid.Rows.Clear();

我试图弄清楚如何从SQLServer发送两个项目(在一列中向上和向下)

以下是我现有的代码:

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();
          }
我需要“Name”和“Subname”位于datagridview的一列中

我需要“Name”和“Subname”位于datagridview的一列中

您可以使用两种方法来实现这一点

第一种方法

从数据库中获取数据时,连接两列:

SELECT name + ' ' + Subname as NameAndSubname, Code, Price from Customers
第二种方法

在应用程序端执行连接:

AppointmentGrid.Rows[n].Cells[0].Value = 
    item[0].ToString() + " " + item[1].ToString();

谢谢你的代码,但是如果我想添加新行怎么办?这个代码会并排添加项目side@Dss请参阅以了解如何做到这一点。感谢您使用DeafaultCellStyle Wraped和autosizerowmode解决了此问题-AllCells@Dss既然你是新来的,有时间请阅读。