简体   繁体   English

GridView上的动态复选框

[英]Dynamic checkbox on gridview

I have an c# aspx web application that is meant to generate most of its components on the fly. 我有一个c#aspx Web应用程序,用于动态生成其大多数组件。 There is this GridView that is meant to display some check boxes. 此GridView旨在显示一些复选框。

The wierd thing is that it does not show the component on run but the namespace, ie System.UI.Webcontrols.CheckBox . 奇怪的是,它不会显示正在运行的组件,而是显示名称空间,即System.UI.Webcontrols.CheckBox

I need to see the checkbox; 我需要查看复选框; how can this be fixed? 如何解决?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" />
    </div>
    </form>
</body>
</html>

[aspx.cs]

namespace ChkBoxOnGridView
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateGridView();
        }
        protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cb = new CheckBox();
                e.Row.Cells[1].Controls.Add(cb);
            }
        }

        private void PopulateGridView()
        {
            GridView1.DataSource = null;
            GridView1.Columns.Clear();

            DataTable dt = new DataTable();

            dt.Columns.Add(new DataColumn("DTA", typeof(System.String)));
            dt.Columns.Add(new DataColumn("Is Visible", typeof(System.Web.UI.WebControls.CheckBox)));

            CheckBox chkbx = new CheckBox();
            chkbx.Checked = true;

            DataRow row1 = dt.NewRow();
            row1["DTA"] = "Some Text";
            row1["Is Visible"] = chkbx;
            dt.Rows.Add(row1);

            foreach (DataColumn col in dt.Columns)
            {
                BoundField bField = new BoundField();
                bField.DataField = col.ColumnName;
                bField.HeaderText = col.ColumnName;
                GridView1.Columns.Add(bField);
            }

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
}

You need to create each object individually in each row using the GridView.RowdataBound event . 您需要使用GridView.RowdataBound事件在每一行中分别创建每个对象。

You can add it using the e.Row.Cells[] 您可以使用e.Row.Cells[]添加它

But, to get sure that you're creating it in the right place, you have to check in this event that you are in a row which is not a header or footer, or pager. 但是,要确保在正确的位置创建它,必须在此事件中检查您是否位于行中,而不是页眉,页脚或寻呼机。 You do this by checking the GridViewRow.RowType Property of the e.Row and check if it's DataRow 您可以通过检查e.Row的GridViewRow.RowType属性并检查其是否为DataRow

 void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{

  if(e.Row.RowType == DataControlRowType.DataRow)
  {
    CheckBox cb = new CheckBox();
    // cb.id = ... and other control setup
    // add your control here:
    e.Row.Cells[0].Controls.Add(cb);
  }
}

Going out on a limb i would say that you can't hold a control in a DataTable. 我会说你不能在DataTable中持有控件。 The [Is Visible] should be a boolean value [可见]应为布尔值

I'd add columns in the On Init event, using a template column. 我会使用模板列在On Init事件中添加列。 See this SO question: Adding dynamic columns to an ASP.NET Gridview 看到这样的问题: 向ASP.NET Gridview添加动态列

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

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