Innova
Innova

Reputation: 4971

placing checkbox in gridview in c#

i need to add checkbox column to my gridview in c#.

i have my code:

               foreach (GridViewRow objRow in GrdDynamicControls.Rows)
                {
                    if (dttableDetails.Columns.Contains(strColumnName))
                    {
                        position = dttableDetails.Columns[strColumnName].Ordinal;

                        if (strtype.Contains("CheckBox"))
                        {
                            try
                            {

                              GrdDynamicControls.Rows[i].Cells.RemoveAt(position);

                                chkCheckBox.ID = strControlName;

                                chkCheckBox.AutoPostBack = true;
                                tcCheckCell.Controls.Add(chkCheckBox);

                                 objRow.Cells.Add(tcCheckCell);
                              //  GrdDynamicControls.Rows[i].Cells.AddAt(position, tcCheckCell);
                            }
                            catch { }
                            chkCheckBox.CheckedChanged += new EventHandler(chkCheckBox_CheckedChanged);



                        }


                    }
            }

but this is overwritting the checkbox for each objrow in gridview. im not able to get the checkbox for that particular column for all the rows in gridview.pls help...

Upvotes: 0

Views: 1924

Answers (3)

Siddharam Padamgond
Siddharam Padamgond

Reputation: 1

Use the TemplateField control in the markup, and define the checkbox in the template instead:

<asp:TemplateField HeaderText="Delete" ItemStyle-Width="39px">
    <ItemTemplate>
        <center>
            <asp:CheckBox ID="chkDelete" runat="server" />
        </center>
    </ItemTemplate>
</asp:TemplateField>

Upvotes: -1

Dan H
Dan H

Reputation: 1828

Try using OnRowDataBound event on the gridview. You can use it to specify what control you want to use for each row. Here's a link that explains it with an example.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

Upvotes: 0

Brian Mains
Brian Mains

Reputation: 50728

Why not use the TemplateField control in the markup, and define the checkbox in the template instead? It would be easier to manage...

<asp:GridVIew ...>

  <Columns>
     <asp:TemplateField ..>
        <asp:CheckBox .. />
     </asp:TemplateField>
  </Columns>
</asp:GridVIew>

And just set everything up in the markup

Upvotes: 1

Related Questions