如何在c#后面的代码中添加带有objectdatasource的gridview?

时间:2015-12-23 05:37:12

标签: c# asp.net

我想用代码创建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();    

怎么办?

1 个答案:

答案 0 :(得分:2)

  1. 您需要设置网格的DataSourceID属性,而不是DataSource
  2. 无需运行DataBind()的{​​{1}}方法。
  3. 确保ObjectDataSource上添加了ObjectDataSourceGridView两个控件。
  4. 试试这个:

    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了解详情。