【问题标题】:how to bind a datagrid?如何绑定数据网格?
【发布时间】:2011-08-06 03:40:32
【问题描述】:

我试图绑定一个数据网格,但我的数据网格的绑定出现问题..

C#代码

DataSet ds = new DataSet();
        DataTable dt = ds.Tables.Add("Source");
        dt.Columns.Add("ID", Type.GetType("System.String"));
        dt.Columns.Add("Desc", Type.GetType("System.String"));


        Insurance oInsurance = new Insurance();
        List<Value> lstValue = oInsurance.Category.ValueList;
        foreach (Value item in lstValue)
        {

            DataRow dr = dt.NewRow();
            dr[0] = item.Key.ToString();
            dr[1] = item.Value.ToString();
            dt.Rows.Add(dr);
        }

        grdCategory.DataSource = ds;
        grdCategory.DataMember = "Source";
        grdCategory.DataTextField = "Desc";
        grdCategory.DataValueField = "ID";
        grdCategory.DataBind();

谢谢

【问题讨论】:

  • 以上没有问题,是什么错误或问题?
  • 详细说明您在绑定网格时遇到的问题
  • 您使用的是DataGrid 还是DataGridView?你不能只把两个标签都扔进去,然后指望人们猜。
  • 请修改您的问题以说明您遇到的确切问题。

标签: c# .net datagrid datagridview


【解决方案1】:

好吧...尝试发布您从中得到的错误...

但是...如果您不需要 DataSet,您可以这样做...

Insurance oInsurance = new Insurance();
List<Value> lstValue = oInsurance.Category.ValueList;

grdCategory.DataSource = lstValue;
grdCategory.AutoGenerateColumns = true; //not sure that's the property
grdCategory.DataBind();

【讨论】:

    猜你喜欢
    • 2011-05-28
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 2011-09-28
    相关资源
    最近更新 更多