Click here to Skip to main content
15,068,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have added the checkbox column through item template in grid view and want to see the value of selected row in the text box

C#
public partial class Default3 : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
       
        DataTable dt = new DataTable();
        //dt.Columns.Add(new DataColumn("select", typeof(bool)));
        dt.Columns.Add("Roll no");
        dt.Columns.Add("Name");
        dt.Columns.Add("marks");
        dt.Rows.Add("D001", "Aditya", "80");
        dt.Rows.Add("D002", "Arun", "75");
        dt.Rows.Add("Doo3", "Nikhil", "85");
        dt.Rows.Add("Doo3", "Nikhil", "85");
        dt.Rows.Add("Doo3", "Nikhil", "85");
        gvnew.DataSource = dt;
        gvnew.DataBind();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GridViewRow row = gvnew.SelectedRow;
        row.Cells[4].Text = TextBox2.Text;  
        row.Cells[5].Text = TextBox3.Text;
    }
    protected void gvnew_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = gvnew.SelectedRow;
        TextBox1.Text= row.Cells[3].Text;
        TextBox2.Text= row.Cells[4].Text;
        TextBox3.Text = row.Cells[5].Text;
        TextBox1.ReadOnly = true;
           
    }

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gvnew.Rows)
        {
            CheckBox chk = (CheckBox)row.FindControl("chkbselect");
            if (chk!=null && chk.Checked)
            {
                TextBox1.Text = gvnew.DataKeys[row.RowIndex].Value.ToString();
                TextBox2.Text = gvnew.Rows[0].Cells[0].Text.ToString();
                TextBox3.Text = row.Cells[5].Text;
                TextBox1.ReadOnly = true;
                //Response.Write("you have selected "+ gvnew.DataKeys[row.RowIndex].Value +"<br />");

            }
        
        }
    }
}
Posted
Updated 16-Aug-12 18:55pm
v5
Comments
   
Tag it: WPF? Forms? Silverlight? what?
--SA
Aditya Asati 17-Aug-12 0:54am
   
forms in asp.net webpage
Aditya Asati 17-Aug-12 0:59am
   
can anyone help me out in the the code above is not having effect.
thanks in advance
Santhosh Kumar Jayaraman 17-Aug-12 0:59am
   
so what happened? any error? or whats the proble,?
Santhosh Kumar Jayaraman 17-Aug-12 1:06am
   
What is this checkbox1? Can you share ur aspx code?
Aditya Asati 17-Aug-12 1:08am
   
code is working fine but when i select the particular checkbox in gridview the value of that particular row is not visible in the textbox.
Aditya Asati 17-Aug-12 1:08am
   
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="gvnew" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" onselectedindexchanged="gvnew_SelectedIndexChanged">
<rowstyle backcolor="#EFF3FB">
<columns>
<asp:TemplateField>
<edititemtemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />

<itemtemplate>
<asp:CheckBox ID="chkbselect" runat="server"
oncheckedchanged="CheckBox1_CheckedChanged" />



<footerstyle backcolor="#507CD1" font-bold="True" forecolor="White">
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<emptydatatemplate>
<asp:CheckBox ID="Chkbselect" runat="server" Checked="True" />

<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<editrowstyle backcolor="#2461BF">
<alternatingrowstyle backcolor="White">


</div>
<asp:Label ID="Label1" runat="server" Text="roll no">
<asp:TextBox ID="TextBox1" runat="server">
<asp:Label ID="Label2" runat="server" Text="name">
<asp:TextBox ID="TextBox2" runat="server">
<asp:Label ID="Label3" runat="server" Text="marks">
<asp:TextBox ID="TextBox3" runat="server">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="update" />
</form>

here is the source part for better understanding
Anumeha Shukla 17-Aug-12 2:13am
   
Is it so that for the first time when the grid is loaded, then by default the checkbox is checked and the problem occurs when you uncheck and check it again that you don't see the values in textbox?
Aditya Asati 17-Aug-12 2:27am
   
by default the check box is unchecked but I want that when I click on particular checkbox the values of the that particular row should be visible in the textboxes below.

1 solution

ASP.NET
<asp:gridview id="gvnew" runat="server" cellpadding="4" forecolor="#333333" gridlines="None" onselectedindexchanged="gvnew_SelectedIndexChanged" xmlns:asp="#unknown"> <asp:templatefield>


///use itemtemplate here

ASP.NET
<itemtemplate>


 <asp:checkbox id="CheckBox1" runat="server" /> <asp:checkbox id="chkbselect" runat="server" oncheckedchanged="CheckBox1_CheckedChanged" /> 

<pagerstyle backcolor="#2461BF" forecolor="White" horizontalalign="Center" /> <asp:checkbox id="Chkbselect" runat="server" checked="True" />

and in c# code
C#
TextBox2.Text = gvnew.Rows[0].Cells[0].Text.ToString();// changed here
Textbox txt=(TextBox)row.findcontrol("textboxid");


and rather than textbox readonly you can use on client side

onkeypress="javascript: return false;" on textbox.

try it


Please Vote if it helps you
   
v2
Comments
Aditya Asati 17-Aug-12 2:36am
   
its not working for me

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