我想用代码创建gridview。我的代码是:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim table As DataTable = New DataTable("SPOT")
Dim foundRow As DataRow = table.Rows.Find("1")
End Sub
End Class
此代码不起作用,以及如何添加TemplateField:
GridView gdvList = new GridView();
gdvList.ID = "gdvList" ;
TemplateField tField = new TemplateField();
BoundField dateBF = new BoundField();
dateBF.DataField = "Date";
gdvList.Columns.Add(dateBF);
BoundField countResponse = new BoundField();
countResponse.DataField = "CountResponse";
gdvList.Columns.Add(countResponse);
ObjectDataSource ods = new ObjectDataSource();
ods.ID = "ods";
ods.TypeName = "Project.BLLQuestion";
ods.SelectMethod = "GetByGroupID";
ods.SelectParameters["GroupID"] = new Parameter("inGroupID", DbType.Int32, "0");
ods.DataBind();
gdvList.DataSource = ods;
gdvList.DataBind();
怎么办?
答案 0 :(得分:2)
DataSourceID
属性,而不是DataSource
。DataBind()
的{{1}}方法。ObjectDataSource
上添加了ObjectDataSource
和GridView
两个控件。试试这个:
Page
根据你的第二个问题。尝试将 ObjectDataSource ods = new ObjectDataSource();
ods.ID = "ods";
ods.TypeName = "Project.BLLQuestion";
ods.SelectMethod = "GetByGroupID";
ods.SelectParameters["GroupID"] = new Parameter("inGroupID", DbType.Int32, "0");
Page.Controls.Add(ods);
GridView gdvList = new GridView();
gdvList.ID = "gdvList" ;
gdvList.DataSourceID = "ods";
BoundField dateBF = new BoundField();
dateBF.DataField = "Date";
gdvList.Columns.Add(dateBF);
BoundField countResponse = new BoundField();
countResponse.DataField = "CountResponse";
gdvList.Columns.Add(countResponse);
Page.Controls.Add(gdvList);
gdvList.DataBind();
替换为TemplateField
。点击here了解详情。