1.页面的代码如下:
1 body> 2 <form id="form1" runat="server"> 3 <div> 4 5 </div> 6 <DIYC:SmartGridView ID="SmartGridView1" runat='server' DataSourceID="ObjectDataSource1" > 7 </DIYC:SmartGridView> 8 <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="Get" TypeName="HuaweiSoftware.Chenlh.UI.ObjData"></asp:ObjectDataSource> 9 </form> 10 </body>
现在要自定义一个类型,然后能够绑定到ObjectDataSource类型上去
2.办法如下:
1 [DataObject] 2 public class ObjData 3 { 4 public ObjData() 5 { 6 } 7 8 [DataObjectMethod(DataObjectMethodType.Select)] 9 public DataTable Get() 10 { 11 DataTable dt = new DataTable(); 12 dt.Columns.Add("ID", typeof(string)); 13 dt.Columns.Add("Name", typeof(string)); 14 dt.Columns.Add("Age", typeof(string)); 15 dt.Columns.Add("Salary", typeof(string)); 16 dt.Columns.Add("Group", typeof(string)); 17 for (int i = 0; i < 666; i++) 18 { 19 DataRow dr = dt.NewRow(); 20 dr[0] = "ID:" + i.ToString().PadLeft(4, '0'); 21 dr[1] = "名字:" + i.ToString().PadLeft(4, '0'); 22 dr[2] = "年龄:" + new Random(i).Next(20, 30); 23 dr[3] = "周薪:" + new Random(i).Next(8000, 9000); 24 dr[4] = "分组:" + i / 7; 25 dt.Rows.Add(dr); 26 } 27 28 return dt; 29 } 30 }
在类型上加上[DataObject]属性,然后在方法上加上DataObjectMethod
然后就可以为ObjectDataSource对象添加数据源配置了,如下图所示:
第一步:
第二步:
第三步: