2011-04-15 78 views
0

我试图绑定一个DataGrid但在我的DataGrid的结合问题..如何绑定数据网格?

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

在上面没有问题,错误或问题是什么? – BugFinder 2011-04-15 08:19:04

+0

问题是什么? – 2011-04-15 08:19:12

+0

指定您在绑定网格时遇到的问题 – gbbosmiya 2011-04-15 08:21:57

回答

1

好...尝试后,你是错误从这个越来越...

但是...如果你不需要的数据集,你可以只是做这样的...

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

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