DatagridView在一列中包含多个项目

时间:2017-11-19 20:47:17

标签: c#

我试图弄清楚如何从sql server发送两个项目(在列内上下)。

这是我现有的代码:

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();
          }

我需要'姓名'和'子名称'在我的datagridview中的一列内。

1 个答案:

答案 0 :(得分:0)

  

我需要'Name'和'Subname'在我的datagridview中的一列内。

你可以使用2种方法来做到这一点。

第一种方法

从数据库获取数据时,请连接2列:

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

第二种方法

在应用程序端进行连接:

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