简体   繁体   English

如何在gridview C#.net中以编程方式添加控制单元?

[英]How to add control cells programmatically in a gridview C# .net?

Every row have to has a dropdownlist and a submit button. 每行必须有一个下拉列表和一个提交按钮。 So I made a List for data. 所以我做了一个数据清单。 I added them like this. 我这样添加他们。 enter image description here //In my code behind 在此处输入图片说明 //在我的代码后面

List<data> listdatainfo = new List<data>();

protected void Button1_Click(object sender, EventArgs e){
SqlDataReader detaillist = comm2.ExecuteReader();
 while (detaillist.Read())
 {
  rmainfo tempinfo = new rmainfo();
  tempinfo.itemdetail= detaillist["itemdetail"].ToString();
  tempinfo.creditmemo= detaillist["creditmemo"].ToString();
   tempinfo.submit= "0";//it will be filled 0 or 1
   listdatainfo .Add(tempinfo);
  }
  loadDataTable();}

//it was referenced from here http://asp.net-informations.com/gridview/without-database.htm //从这里http://asp.net-informations.com/gridview/without-database.htm引用了它

private void loadDataTable()
    {
        DataSet ds = new DataSet();
        DataTable dt;
        dt = new DataTable();
        DataColumn itemdetail;
        DataColumn creditmemo ;         
        CommandField submit = new CommandField();
        submit.EditText = "Edit";
        submit.ShowEditButton = true;

        itemdetail= new DataColumn("itemdetail",Type.GetType("System.String"));
        creditmemo = new DataColumn("creditmemo ",Type.GetType("System.String"));
        submit = new CommandField();
        dt.Columns.Add(itemDetail);
        dt.Columns.Add(creditMemo);
        dt.Columns.Add("submit"); //it's for submit button

        foreach (data tempinfo in listdatainfo )
        {
            DataRow dr;               
            dr = dt.NewRow();
            dr["Item Detail"] = tempinfo.itemDetail;
            dr["Credit Memo"] = tempinfo.creditMemo;
            dr["submit"] = submit;
            dt.Rows.Add(dr);
        }
        ds.Tables.Add(dt);
        GridView2.DataSource = ds.Tables[0];
        GridView2.DataBind();    
       }}

public class data
    {

        public string itemDetail { get; set; }
        public string creditMemo { get; set; }
        public string submit { get; set; }
    }

As i expected, this line occured an error this line. 如我所料,这条线发生了错误。 dr["submit"] = submit; dr [“ submit”] =提交;

How can i add a button each row? 如何在每行添加一个按钮? or any component? 或任何组件? It was easier in classic asp..... Please help. 在经典asp中更容易.....请帮助。

您必须在GridView模型中添加按钮,而不是为其添加数据表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM