向gridview动态添加复选框 [英] add checkbox to gridview dynamically

查看:106
本文介绍了向gridview动态添加复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态地向gridview添加一个复选框。我添加了一个静态名为selectCheckbox的复选框,但我清除了另一个函数中的所有列,如果它等于null,则需要重新添加另一个复选框。在此先感谢!



Im trying to add a checkbox to a gridview dynamically. I have added a checkbox statically called selectCheckbox, but i clear all columns in another function and need to re-add another checkbox if it equals null. Thanks in advance!

protected void mainGridView_RowCreated(Object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow &&
           (e.Row.RowState == DataControlRowState.Normal ||
            e.Row.RowState == DataControlRowState.Alternate))
        {
            CheckBox selectCheckbox = (CheckBox)e.Row.Cells[1].FindControl("selectCheckbox");

            if (selectCheckbox == null)
            {
                selectCheckbox = new CheckBox;  //this is where i need to add a checkbox to the gridview!!!              
            }
           
        }
    }

推荐答案

您可以使用RowDataBound事件下面的代码示例供您参考

You can use RowDataBound event for that below code sample for your reference
protected void mainGridView_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow &&
             (e.Row.RowState == DataControlRowState.Normal ||
              e.Row.RowState == DataControlRowState.Alternate))
           {

               if (e.Row.Cells[1].FindControl("selectCheckbox") == null)
               {
                   CheckBox selectCheckbox = new CheckBox();
                   //Give id to check box whatever you like to
                   selectCheckbox.ID = "newSelectCheckbox";
                   e.Row.Cells[1].Controls.Add(selectCheckbox);

               }
           }
       }


试试这段代码...



try this code...

void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{

  if(e.Row.RowType == DataControlRowType.DataRow)
  {
    CheckBox cbx = new CheckBox();    
    // bind checkbox control with gridview :
    e.Row.Cells[0].Controls.Add(cbx);
  }
}





确保复选框的位置。



be sure about the position of the check box.


这篇关于向gridview动态添加复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆