locked
How to add a checkbox in Gridview ??? RRS feed

  • Question

  • User-1600238721 posted

    Can someone please teach me how to add a checkbox in gridview?

    i added from the add new columns,but the checkbox is not fuctioning when i run my page,means it can't be check

    can someone please teach me in detail?coz i am new in ASP.Net 2.0 ( Using Visual Studio 2005)

    and if possible,i wan to retrieve 1 of the column value of the database row which being checked

    can someone teach me this as well?

    thanks[:)]

    Friday, June 22, 2007 12:13 AM

Answers

  • User-412682272 posted

    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:)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 22, 2007 2:48 AM

All replies

  • User1593896619 posted

    Hello,

    Maybe this article could help you, since the steps are a little bit too complex to explain in a few lines: http://support.microsoft.com/kb/306227

    Hope it helps

    Friday, June 22, 2007 1:07 AM
  • User-412682272 posted

    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:)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 22, 2007 2:48 AM
  • User-1600238721 posted

    Thanks both of u

    i already solve my problem.

    but still facing another problem.i wish to get the data from the gridview row which being checked

    i already open a post which pending for approval.hope u both still can give me some idea.thanks[:)]

    Friday, June 22, 2007 3:07 AM
  • User-412682272 posted

    You may want to convert everything in your GridView into TemplateFields,

    after that, you will need to FindControl each of those TemplateField controls in the GridView. Finding a control in a GridView is like this:

    Dim myLabel As New Label
    ClusterName = GV.Rows(1).Cells(2).FindControl("myLabelinTheGridViewTemplateField")

     

     Ill wait for your pending post, then ill discuss further over there :)

     

    Cheers

    Friday, June 22, 2007 3:18 AM
  • User-1600238721 posted

    Convert everything into TempleteFields? how to do that?can u tell me detail about that?

    this is the post  http://forums.asp.net/t/1125079.aspx

    thanks for helping [:)]

    Friday, June 22, 2007 3:29 AM
  • User1593896619 posted

    I generally always use templateFields too since i generally don't want things to be line up in columns.

    1-in the smart Gridview Tasks that pops open when you drop a gridview on the form , Click on Edit Columns.

    2- In the listbox on the top left side , click on TemplateField and push the add button. The field is added to the bottom left list.

    3- you can set a couple of the field properties in the right side  (the Header text might be a good start.)

    4- When you hit ok, the popup is still open, click on Edit Templates... and customize your ItemTemplate. (depending on what you want the EditItemTemplate and the ItemTemplate to look like ).

     

    Friday, June 22, 2007 11:08 AM
  • User2098908334 posted

    Pickick a GridView, Connect it to a table.

    - Go to the 'Edit-Columns' of the GridView.
    - Add a 'template-field'

    - Hit OK.

    - Now, go to the 'Edit-Template' of the GridView.
    - In the 'Column[0]' of the Item-Template, drag-drop a CheckBox
    - Check out whats the ID of the CheckBox (must be CheckBox1)

    -- That's it.
    Now Stop 'Editing Template'

     In the Code-Behind use the following code :

    foreach (GridViewRow gr in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)gr.Cells[0].FindControl("CheckBox1");
                if (cb.Checked)
                {
                      // do ur job here

                }
            }

    thus u can access ur checkbox by iterating thru the GridView Rows.

     

    hope this explained alot for u

    Sunday, April 27, 2008 11:28 AM