【问题标题】:DatagridView multi items in one ColumnDatagridView 一列中的多项
【发布时间】:2017-11-20 05:19:17
【问题描述】:

我正在试图弄清楚如何从 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 的一列中。

【问题讨论】:

    标签: c#


    【解决方案1】:

    我需要将“名称”和“子名称”放在我的 datagridview 的一列中。

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

    第一种方法

    当您从数据库中获取数据时,将 2 列连接起来:

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

    第二种方法

    在应用端做连接:

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

    【讨论】:

    • 谢谢你的代码有效,但如果我想添加新行怎么办??这段代码并排添加项目
    • @Dss 请参阅this 了解如何操作。
    • 谢谢你用DeafaultCellStyle Wraped和autosizerowmode -AllCells解决了
    • @Dss 既然是新人,有空请看this
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多