Feb 15th, 2012, 10:42 PM
#1
Thread Starter
Fanatic Member
Checkbox in GridView
i use this code to delete records in datagridview
Code:
// Iterate through the Products.Rows property
bool atLeastOneRowDeleted = false;
foreach (GridViewRow row in grdMessages.Rows)
{
if (((CheckBox)row.FindControl("chkMessage")).Checked == true)
{
atLeastOneRowDeleted = true;
}
}
But I encounter this message;
The varialbe 'atLeastOneRowDeleted' is assigned but its value is never used
and the 'atLeastOneRowDeleted' return False result even if i check a checkbox on the grid
Feb 16th, 2012, 06:51 AM
#2
Re: Checkbox in GridView
1.Where do you check the values.
2.Do you check the checked property value before binding the gridview.
3.Normally when the page is posted and the gridview is binded the checkbox lost its value
Please mark you thread resolved using the Thread Tools as shown
Feb 16th, 2012, 11:33 PM
#3
Thread Starter
Fanatic Member
Re: Checkbox in GridView
1.Where do you check the values.
I check from checkbox inside datagridview
2.Do you check the checked property value before binding the gridview.
i check it after binding then put the code in a button
3.Normally when the page is posted and the gridview is binded the checkbox lost its value
Feb 17th, 2012, 12:30 AM
#4
Re: Checkbox in GridView
3.Normally when the page is posted and the gridview is binded the checkbox lost its value[/QUOTE]
Post the entire coding
Please mark you thread resolved using the Thread Tools as shown
Feb 17th, 2012, 01:52 AM
#5
Thread Starter
Fanatic Member
Re: Checkbox in GridView
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindData();
}
Code:
private void BindData()
{
SqlConnection oConn = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
SqlDataAdapter cmdGrid = default(SqlDataAdapter);
DataSet dsGrid = new DataSet();
cmdGrid = new SqlDataAdapter("SELECT * FROM Messages Order By MessageSent DESC", oConn);
cmdGrid.SelectCommand.CommandTimeout = 300;
cmdGrid.Fill(dsGrid, "Messages");
DataView dv = new DataView(dsGrid.Tables["Messages"]);
grdMessages.DataSource = dv;
grdMessages.DataBind();
}
Code:
protected void btnDelete_Click(object sender, EventArgs e)
{
// Iterate through the Products.Rows property
bool atLeastOneRowDeleted = false;
foreach (GridViewRow row in grdMessages.Rows)
{
if (((CheckBox)row.FindControl("chkMessage")).Checked == true)
{
atLeastOneRowDeleted = true;
}
}
}
Feb 17th, 2012, 11:23 AM
#6
Re: Checkbox in GridView
can you post the aspx code, specially the id of checkbox...
__________________
Rate the posts that helped you
Feb 18th, 2012, 11:59 AM
#7
Re: Checkbox in GridView
1.Have you placed the delete button on each row are separately ?
2. Did you debug the code. Does it find the checkbox and its state ?
foreach (GridViewRow row in grdMessages.Rows)
{
if (((CheckBox)row.FindControl("chkMessage")).Checked == true)
{
atLeastOneRowDeleted = true;
}
}
Please mark you thread resolved using the Thread Tools as shown
Feb 20th, 2012, 02:33 AM
#8
Thread Starter
Fanatic Member
Re: Checkbox in GridView
CheckBox ID is: chkMessage
1. The delete button was out the datagridview.
2. There is not bug.. it uncheck all the checkbox after i Clicked the delete button.
Feb 20th, 2012, 02:34 AM
#9
Re: Checkbox in GridView
Hello,
The error/warning that you are getting is because you never actually use the local variable that you are creating. Yes, you assign a value to it, but you never do anything with the value.
Are you going to return that value to somewhere? Or what decision are you going to take based on it's value?
Gary
Feb 20th, 2012, 03:31 AM
#10
Thread Starter
Fanatic Member
Re: Checkbox in GridView
what i want to happen is when I check the checkbox and click the delete button.. the selected items will be deleted too..
Feb 20th, 2012, 04:18 AM
#11
Re: Checkbox in GridView
It will be more convinient to us if you can post the entire page code (including html markup code) as we can’t see any code that actually delete the records
Also try by changing
Code:
row.FindControl("chkMessage")).Checked
With
Code:
row.Cells[index_of_column_having_Checkbox].FindControl("chkMessage")
__________________
Rate the posts that helped you
Feb 20th, 2012, 09:22 AM
#12
Thread Starter
Fanatic Member
Re: Checkbox in GridView
thanks, attached with this email are the aspx files..
Attached Files
Feb 21st, 2012, 02:20 AM
#13
Re: Checkbox in GridView
Hello,
The code that you have works perfectly, as long as you make one important change.
In the PageLoad event, you are re-binding the GridView every time the pages loads. As a result, you are losing the changes that the user has made on the client, i.e. when they check the CheckBox. Rather than re-binding on every page load, you want to make sure that you are only binding when the page first loads.
This can be achieved using the following check:
Code:
if (!this.IsPostBack)
{
SqlConnection oConn = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
.... <rest of your code goes here>
}
Hope that helps!
Gary
Feb 21st, 2012, 07:03 AM
#14
Re: Checkbox in GridView
Thanks Gary.
That's what I was referring to in post #4
Please mark you thread resolved using the Thread Tools as shown
Feb 22nd, 2012, 07:38 AM
#15
Re: Checkbox in GridView
Hey Dana,
I thought that is what you were getting at. It was only when we saw the whole code that we got to see what the problem was.
Gary
Feb 23rd, 2012, 03:59 AM
#16
Please mark you thread resolved using the Thread Tools as shown
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width