如何绑定数据网格?

时间:2011-04-15 08:13:25

标签: c# .net datagrid datagridview

我试图绑定数据网格但是我的数据网格绑定存在问题..

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

由于

1 个答案:

答案 0 :(得分: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();