0

This is a basic question but i didn't find appropriate answers : I have a dataset that is shown in dataGridview and it contains a column Is_Alarm of type bit (boolean) , i want to insert a Select all checkbox in that column.

I have seen many solutions but they are all about inserting a new checkbox in datagridView . What i want is insert it after columns are shown , here's my code :

SqlDataAdapter adap= new SqlDataAdapter(select_query,con);
                ds = new DataSet();
                adap.Fill(ds, "Event_test");
                dataGridView1.DataSource = ds.Tables[0];
Ahmed Aekbj
  • 91
  • 1
  • 12

1 Answers1

0

I had same issue what I did might be useful for you

This is code used for gridview

<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True">               
        <Columns>
            <asp:TemplateField>
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Next I Loaded data( MyTable field had id,username,email etc)

protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            con.Open();
            SqlDataAdapter adap = new SqlDataAdapter("Select * From UserInfo", con);
            DataSet ds = new DataSet();
            adap.Fill(ds);
            con.Close();             
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }

    }

For getting Ids of selected records I used few lines which is described here http://www.aspsnippets.com/Articles/GridView-with-CheckBox-Get-Selected-Rows-in-ASPNet.aspx and modified in this way

protected void btnCheckSelected_Click(object sender, EventArgs e)
    {

        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[0].FindControl("CheckBox1") as CheckBox);
                if (chkRow.Checked)
                {
                    string ids = row.Cells[1].Text;

                    ListBox1.Items.Add(ids);
                }
            }
        }
    }
Shriram Panchal
  • 1,996
  • 1
  • 20
  • 23