Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I get the value in the gridview which the checkbox is checked
in my gridview one column I created checkboxs ,when i click that checkbox of a row
then the state is save into database and when reopen the site then the saved state
is show into the gridview.....



in row command i successfully save bit1 for chek and bit0 for uncheck in a table.. but how to get this saved state in my gridview..plzzz tell me
Posted
Updated 27-Sep-12 19:06pm
v2
Comments
Sandeep Mewara 27-Sep-12 12:24pm    
What is the issue when you try?

protected void gvCheck_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = ViewState["chk"] as DataTable;

if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)e.Row.FindControl("chk");
Boolean flag = Convert.ToBoolean(dt.Rows[e.Row.RowIndex]["Chk"].ToString());// find column of table which contain value for check box
if (flag == true)
{
chk.Checked = true;
}

}<
}

i hope this will help u
 
Share this answer
 
Comments
Rashid Choudhary 28-Sep-12 2:54am    
this code is give a null refrence exception


protected void Grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = ViewState["checkbox1"] as DataTable;

if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox checkbox1 = (CheckBox)e.Row.FindControl("checkbox1");
Boolean check_bit = Convert.ToBoolean(dt.Rows[e.Row.RowIndex]["Checkbox1"].ToString());//null reference exeption
if (check_bit == true)
{
checkbox1.Checked = true;
}

}
}
solanki.net 28-Sep-12 3:05am    
u got error in this line

Boolean check_bit = Convert.ToBoolean(dt.Rows[e.Row.RowIndex]["Checkbox1"].ToString());//null reference exeption

["Checkbox1"] is not checkbox id ,it is table column name so give column name here .
pls tell me it solved your problem or not ?
Rashid Choudhary 28-Sep-12 3:08am    
i give column name but same exeption is occured
Rashid Choudhary 28-Sep-12 3:10am    
i send u full code plzzz tell me what prob is occured

solanki.net 28-Sep-12 3:11am    
send me your entire code and database table column name
C#
foreach(GridViewRow objRow in GridView.Rows)
{
   CheckBox objCheckbox = ((CheckBox )objRow.Cells[0].Controls[0]);
   if(objCheckbox.Checked)
   
}
 
Share this answer
 
v2
Please refer,

DataKeyNames

to know how to use datakeynames at rowcommand.

it will be very helpful to you........
 
Share this answer
 
v2
Hi Rashid,

You can use Row command event of grid view. I have put the row command event in your grid. you are having p_id and u_id . get that ids at row command and the save it to database. retrieve from database when you want.
ASP.NET
<asp:GridView ID="Grid1" runat="server" BackColor="#DEBA84"
        BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
        CellSpacing="2" Width="90%" Height="10%"
        AutoGenerateColumns="False"
                        AllowPaging="True" AllowSorting="True" PageSize="5"
            onpageindexchanging="Grid1_PageIndexChanging"
            onrowediting="Grid1_RowEditing" onrowupdating="Grid1_RowUpdating" OnRowCommand="Grid1_RowCommand"
            DataKeyNames ="P_id, U_id" >
 
Share this answer
 
v2
Comments
Rashid Choudhary 27-Sep-12 8:31am    
plz send me a code which is used in row command
Rashid Choudhary 28-Sep-12 1:06am    
in row command i successfully save bit1 for chek and bit0 for uncheck in a table.. but how to get this saved state in my gridview..plzzz tell me
Hi Rashid,

You can add a column in grid say checkbox, now each checkbox will have id of the current row shown to user.

Now when user checks that checkbox an event will be fired so at that time you can store id which is associated with the checkbox to some hiddenfield or you can made postback and call save state method from server side if it is just required on check event of checkbox.

and when you load the grid just show the value as per check/uncheck into checkbox.

Let me know if you have a question.
 
Share this answer
 
Comments
Rashid Choudhary 27-Sep-12 8:08am    
whose event will be use i m?
Rashid Choudhary 27-Sep-12 8:10am    
here is my source code

<asp:GridView ID="Grid1" runat="server" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
CellSpacing="2" Width="90%" Height="10%"
AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True" PageSize="5"
onpageindexchanging="Grid1_PageIndexChanging"
onrowediting="Grid1_RowEditing" onrowupdating="Grid1_RowUpdating"
DataKeyNames ="P_id, U_id" >
<rowstyle backcolor="#FFF7E7" forecolor="#8C4510">
<footerstyle backcolor="#F7DFB5" forecolor="#8C4510">
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<columns>
<asp:TemplateField HeaderText="Productname">
<itemtemplate>
<asp:Label ID="lblprodn" runat="server" Text ='<%#bind("Product_name") %>'>
       



<asp:TemplateField HeaderText="ProductQuantity">
<itemtemplate>
<asp:Label ID="lblprodq" runat="server" Text ='<%#bind("Product_quan") %>'>
       



<asp:TemplateField HeaderText="ProductDeliveredOn">
<itemtemplate>
<asp:Label ID="lbldate" runat="server" Text ='<%#bind("Delivery_date") %>'>
       



<asp:TemplateField HeaderText="Product Ready">
<itemtemplate>
<asp:CheckBox ID="checkbox1" runat="server" Checked="false"/>
<%-- <asp:Button ID="Button2" runat="server" Text="ProductReady" CommandName="Update" onclick="Button2_Click" CausesValidation="False"
OnClientClick="return confirm('information send to management for confirmation');"/>--%>



<asp:TemplateField HeaderText="Date Extend" >
<itemtemplate>
<asp:Label ID ="lbl1" runat="server" Text='<%#Bind("Extend_date") %>'>

<edititemtemplate>
<asp:TextBox ID="txtlbl" runat="server" class= "datepicker" Text='<%# Bind("Extend_date")%>'>


<asp:TemplateField HeaderText="Operation">
<itemtemplate>
<asp:Button ID="btnedit" runat="server" CommandName="Edit" Text="Edit" />

<edititemtemplate>
<asp:Button ID="btnupdate" runat="server" CommandName="Update" Text="Update" />



<rowstyle backcolor="#FFF7E7" forecolor="#8C4510">
<footerstyle backcolor="#F7DFB5" forecolor="#8C4510">
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />

nikunjmochi 27-Sep-12 8:16am    
On which event you have to store the state of checkbox?
Rashid Choudhary 27-Sep-12 8:18am    
on grid1_rowdatabound
Rashid Choudhary 27-Sep-12 8:19am    
plzzz solved it in ur way...

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