Click here to Skip to main content
16,018,057 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have added like this...
C#
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();

               checkColumn.AutoSizeMode =
                   DataGridViewAutoSizeColumnMode.DisplayedCells;
               checkColumn.CellTemplate = new DataGridViewCheckBoxCell();
               checkColumn.FalseValue = true;
               checkColumn.HeaderText = "SelectCompany";
               checkColumn.Name = "CheckBoxes";
               checkColumn.TrueValue = true;
               checkColumn.FalseValue = false;
               dgvGetCompanyDetails.Columns.Add(checkColumn);

But I am not able to check the checkboxes
Posted
Updated 21-Jun-12 19:14pm
v2

XML
Hi,

you can use TemplateFields for the GridView:

                 <asp:GridView ID="GridView1" runat="server">

                   <Columns>

                        <asp:TemplateField >
                             <ItemTemplate>
                                 <asp:CheckBox ID="myCheckBox" runat="server" />
                             </ItemTemplate>
                         </asp:TemplateField>

                  </Columns>

               </asp:GridView>



Hope this helps:)
 
Share this answer
 
Comments
CHill60 30-Jul-15 6:15am    
That's a GridView not a DataGridView - there is a difference. That and the post is over 3 years old
Once you have your data in the grid, you can loop through the rows and check each box like this:
C#
foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        row.Cells[CheckBoxColumn1.Name].Value = true;
    }

The Click event you can get like this:

C#
private void button1_Click(object sender, EventArgs e)
    {
        List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (Convert.ToBoolean(row.Cells[CheckBoxColumn1.Name].Value) == true)
            {
                rows_with_checked_column.Add(row);
            }
        }
        // Do what you want with the check rows
    }


Refer:
Good example on MSDN[^]
Check out Detailed description[^] here.
 
Share this answer
 
Comments
Member 9364222 4-Oct-12 6:52am    
hi,
i have a gridview with a checkbox column,databound column and a textbox template column.Initially the text box column is not visible. when i click the checkbox in a row the corresponding row's text box should be visible.

my gridview
<asp:GridView ID="gvwProduct" OnPageIndexChanging="gridView_PageIndexChanging" runat="server"
AutoGenerateColumns="False" EnableModelValidation="True"
Width="259px" Height="130px"> <%--onselectedindexchanged="gvwProduct_SelectedIndexChanged">--%>
<columns> <asp:TemplateField HeaderText="Check" >
<itemtemplate>
<asp:CheckBox ID="chkbxPrd" AutoPostBack="true" runat="server" />



<asp:BoundField DataField="cService_Desc" HeaderText="Product/Service" runat="server" />

<asp:TemplateField HeaderText="Contract Value">
<itemtemplate>
<asp:Label ID="lblPrd" runat="server" Visible="true"/>
<asp:TextBox ID="txtPrd" runat="server" Visible="false" />


is there any solution for this
Chavakorn 19-May-13 8:24am    
Sorry I have a question that this Code for Gridview or DataGridview ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900