Hi Everyone,

I have a gridview

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" DataKeyNames="Id"ShowHeaderWhenEmpty="true">

inside the gridview there is a column with checkbox and another label.

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

 <asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>

</ItemTemplate>
</asp:TemplateField>

My question is how do i retrieve that particular row of data according to the Checkbox checked.

Like for exmaple, the gridview i check the checkbox which is in the row 3. How do i retrieve the row 3 data?

Everyone, Please help me.. i have been searching for days and found same thing on the net. Would you provide me with some guideline? I really need some guideline to assist me..

Your help is great appreciated.

If you have any enquire Please post below. i will answer as soon as possible.

Thanks,
Esther.

Recommended Answers

All 7 Replies

hii..

to display the data you have selected in checkbox can be populate it in checkbox changed event as follows

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    //TextBox tb = ((Control)sender).NamingContainer.FindControl("TextBox2") as TextBox;
    CheckBox chk = ((Control)sender).NamingContainer.FindControl("CheckBox1") as CheckBox;

    string requiredtext = chk.Text;
  Response.Write(requiredtext);
}

Hi kamilacbe,

Thanks for your response. I put your code in my project. i couldn't display the result in a label. I placed a breakpoint at the CheckBox1_CheckedChanged() the code did not execute till the breakpoint.

The checkbox is inside the gridview, is it possible?

Thanks,
Esther

hi..

yup it is ..as you have done in your sample inside your item template tag..then pass the column which has to be populated as the text for that checkbox...then in your change event go for this c# part .


Do u want the snippet for design too

<asp:GridView ID="GridView1" runat="server" BackColor="White" 
            BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
                onrowdatabound="GridView1_RowDataBound" 
                onselectedindexchanged="GridView1_SelectedIndexChanged" 
                    onrowupdating="GridView1_RowUpdating" 
            onrowediting="GridView1_RowEditing">
                    <RowStyle BackColor="White" ForeColor="#003399" />
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate >
                                <asp:CheckBox ID="CheckBox1" runat="server" Font-Italic="True" Text ='<%#Eval("name")%>'
                                    oncheckedchanged="CheckBox1_CheckedChanged" AutoPostBack="True" />
                                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:CommandField ShowEditButton="True" />
                    </Columns>
                    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                    <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                    <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                    <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                </asp:GridView>

In the c# code uncomment the textbox and pass the value to textobject you can find the output in gridview textbox. or go for response .write...

Hi
Thanks for the reply. I have tried your code but unable to display the checkbox's text in the textbox2.text...

I have tried your code on another project that uses datasource to populate in the gridview and i am able to display the checkbox's text in the textbox2.text..
But i am not supposed to use DataSource.

Do you mind updating me if you are using datasource for populating your gridview data?

Sorry for my improper english.

Thanks =)

hii..

I use datasource to populate the gridview...can you be precise if u dont mind...


this is how i bounded the data...guess you would know this

if (!IsPostBack)
        {
            con.ConnectionString = constr;
            con.Open();
            SqlDataAdapter ada = new SqlDataAdapter("select * from Table3", con);
            ada.Fill(ds, "Table3");
            DataGrid1.DataSource = ds;
            DataGrid1.DataBind();
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

Hi,

Really thanks for your valuable response and thanks for your precious time too..

Thanks,
Esther

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.