Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Gridview
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 27-Sep-12 1:55am
Edited 27-Sep-12 19:06pm
v2
Comments
Sandeep Mewara at 27-Sep-12 12:24pm
   
What is the issue when you try?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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
  Permalink  
Comments
Rashid Choudhary at 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 at 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 at 28-Sep-12 3:08am
   
i give column name but same exeption is occured
Rashid Choudhary at 28-Sep-12 3:10am
   
i send u full code plzzz tell me what prob is occured
 
solanki.net at 28-Sep-12 3:11am
   
send me your entire code and database table column name
Rashid Choudhary at 28-Sep-12 3:12am
   
protected void Grid1_RowCommand1(object sender, GridViewCommandEventArgs e)
{
 
foreach (GridViewRow row in Grid1.Rows)
{
CheckBox chkBx = (CheckBox)row.FindControl("checkbox1");
 
GridViewRow myRow = (GridViewRow)chkBx.Parent.Parent; // the row
GridView myGrid = (GridView)sender; // the gridview
string ID = myGrid.DataKeys[myRow.RowIndex].Values[0].ToString();
if (chkBx.Checked == true)
{
string s = "update Product_order set check_bit='1' where P_id='" + ID + "'";
cmd = new SqlCommand(s, con);
cmd.ExecuteNonQuery();
 
}
}
 
}
 
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]["check_bit"].ToString());
if (check_bit == true)
{
checkbox1.Checked = true;
}
 
}
}
and column name is check_bit
solanki.net at 28-Sep-12 3:29am
   
null reference exception means your table not contain records if it is contain then tell me what is type of column ["check_bit"] in database .
Rashid Choudhary at 28-Sep-12 3:29am
   
varchar
solanki.net at 28-Sep-12 3:36am
   
firstly u check easily
Convert.ToBoolean(dt.Rows[e.Row.RowIndex]["check_bit"].ToString());
i am converting in boolean and y r taking varchar in database so how is it possible so modify this line like that
 
string check_bit=(dt.Rows[e.Row.RowIndex]["check_bit"].ToString());
 
if (check_bit == "1")
{
checkbox1.Checked = true;
}
 
tell me that in your table data is coming or not from database
Rashid Choudhary at 28-Sep-12 3:47am
   
i use ur code but same exeption
Rashid Choudhary at 28-Sep-12 3:31am
   
by using row command .i pass a value 1 for checkbox checked to database.
solanki.net at 28-Sep-12 4:24am
   
tell me table contain record or not
DataTable dt = ViewState["checkbox1"] as DataTable;
 
if(dt.Rows.Count>0 && dt!=null)
{
then write code here .
}
put break point and check table contain record or not
Rashid Choudhary at 28-Sep-12 4:27am
   
DataTable dt = ViewState["checkbox1"] as DataTable;
 
if(dt.Rows.Count>0 && dt!=null)//null reference exception
{
then write code here .
}
solanki.net at 28-Sep-12 4:31am
   
because table does not exist .so tell me,after saving record in database, how u getting record from database for binding in grid
Rashid Choudhary at 28-Sep-12 4:33am
   
Product_name varchar(50) Checked
Product_quan int Unchecked
Delivery_date date Checked
Uname varchar(50) Checked
P_id int Unchecked
Extend_date date Checked
st_bit int Checked
flag int Checked
check_bit varchar(10) Checked
Unchecked
solanki.net at 28-Sep-12 4:39am
   
i asked after saving record what code u write for getting record from database .because when on page load or page refresh ,u will have to bind grid .what code u r writing to get record from database ?
Rashid Choudhary at 28-Sep-12 4:46am
   
protected void Button1_Click(object sender, EventArgs e)
{
buildgrid();
Grid1.Visible = true;
gvDetails.Visible = false;
}
public void buildgrid()
{

 
da = new SqlDataAdapter("bofc_prod", con);
ds1 = new DataSet();
da.Fill(ds1);
Grid1.DataSource = ds1.Tables[0];
Grid1.DataBind();
Grid1.Visible = true;
}
 
and in proc
 

select a.U_id,b.P_id, b.Product_name,b.Product_quan,b.check_bit,b.Delivery_date,b.Extend_date from Client_registration_tbl a , Product_order b where a.Uname= b.Uname
solanki.net at 28-Sep-12 4:50am
   
public void buildgrid() { da = new SqlDataAdapter("bofc_prod", con); ds1 = new DataSet(); da.Fill(ds1);
viewstate["table"]=ds1.Tables[0]//Add this line
Grid1.DataSource = ds1.Tables[0]; Grid1.DataBind(); Grid1.Visible = true;
 
}
 
// And modify your existing code on rowdatabound event
 
DataTable dt = ViewState["table"] as DataTable; if(dt.Rows.Count>0 && dt!=null) {
then write code here .
}
 
Rashid Choudhary at 28-Sep-12 5:04am
   
thannnnxxxxxxxxx to help
solanki.net at 28-Sep-12 5:07am
   
Enjoyyyyyyyyyyyy
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

foreach(GridViewRow objRow in GridView.Rows)
{
   CheckBox objCheckbox = ((CheckBox )objRow.Cells[0].Controls[0]);
   if(objCheckbox.Checked)
   
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Please refer,
 
DataKeyNames
 
to know how to use datakeynames at rowcommand.
 
it will be very helpful to you........
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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: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" >
  Permalink  
v2
Comments
Rashid Choudhary at 27-Sep-12 8:31am
   
plz send me a code which is used in row command
Rashid Choudhary at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Rashid Choudhary at 27-Sep-12 8:08am
   
whose event will be use i m?
Rashid Choudhary at 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" >


<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />

<asp:TemplateField HeaderText="Productname">

<asp:Label ID="lblprodn" runat="server" Text ='<%#bind("Product_name") %>'>
       



<asp:TemplateField HeaderText="ProductQuantity">

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



<asp:TemplateField HeaderText="ProductDeliveredOn">

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



<asp:TemplateField HeaderText="Product Ready">

<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" >

<asp:Label ID ="lbl1" runat="server" Text='<%#Bind("Extend_date") %>'>


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


<asp:TemplateField HeaderText="Operation">

<asp:Button ID="btnedit" runat="server" CommandName="Edit" Text="Edit" />


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





<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />

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

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

  Print Answers RSS
0 Maciej Los 645
1 OriginalGriff 453
2 CPallini 239
3 CHill60 180
4 Peter Leow 175
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 4,898
2 Maciej Los 3,159
3 Peter Leow 3,129
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 28 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100