I have a column of checkboxes to select the records in the gridview but i am struggling to determine which checkboxes were checked on postback caused by button click.I used the following code but it doesnt work.
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("Chkgridselect");
if (cb.Checked)
{
int id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
}
}
}
Can anyone please rectify the above coding?
This is what i do and it is working:
foreach (GridViewRow row in GridView1.Rows)
{
// Access the CheckBox
CheckBox cb = (CheckBox)row.FindControl("Chkgridselect");
if (cb != null && cb.Checked)
{
//dostuff
}
}
Related
I have found several related articles and tried them, but couldn't solve the problem. I have a checkbox column in the datagridview of my winForm application. I want to select multiple rows by checking the checkboxes of the adjacant rows and perform some operation on selected rows. But my rows are not getting selected. My code is:
this.dgvLoadTable.CellClick += new DataGridViewCellEventHandler(dgvLoadTable_CellClick);
private void dgvLoadTable_CellClick(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dgvLoadTable.Rows)
{
//If checked then highlight row
if (Convert.ToBoolean(row.Cells["Select"].Value))// Select is the name
//of chkbox column
{
row.Selected = true;
row.DefaultCellStyle.SelectionBackColor = Color.LightSlateGray;
}
else
row.Selected = false;
}
}
What I'm doing wrong here?
You need to handle the CellValueChanged event instead of CellClick one:
private void dgvLoadTable_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
foreach (DataGridViewRow row in dgvLoadTable.Rows)
{
if (row.Cells[3].Value != null && row.Cells[3].Value.Equals(true)) //3 is the column number of checkbox
{
row.Selected = true;
row.DefaultCellStyle.SelectionBackColor = Color.LightSlateGray;
}
else
row.Selected = false;
}
}
And add the CurrentCellDirtyStateChanged event also:
private void dgvLoadTable_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dgvLoadTable.IsCurrentCellDirty)
{
dgvLoadTable.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
I want to count the number of checkboxes which are checked on my asp.net page and if the count = 5 then to change the button state from disabled to enabled.
I don't understand because when I clear the check in one checkbox ( from 5 checkboxes checked to 4 checkboxes checked ) the button state is enabled and not change state in disabled.
My code below.
I would greatly appreciate any help you can give me in working this problem.
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkTest = (CheckBox)sender;
GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
int count = 0;
if (chkTest.Checked)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("chkSelect");
count++;
if (chk.Checked && count == 5)
{
btnUpdate.Enabled = true;
btnUpdate.CssClass = "enabledImageButton";
}
}
}
}
EDIT 1
Page_Load code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewBind();
}
}
Check the count after the loop and it should work as expected:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkTest = (CheckBox)sender;
GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
int count = 0;
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("chkSelect");
if (chk.Checked)
{
count++;
}
}
if(count==5)
{
btnUpdate.Enabled = true;
btnUpdate.CssClass = "enabledImageButton";
}
else
{
btnUpdate.Enabled = false;
btnUpdate.CssClass = "disabledImageButton";
}
}
If you un-check then it will not go in this
if (chkTest.Checked)
{
when you remove check then you need to do calculation also.
You dont have code to disable the button. Try the following code
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkTest = (CheckBox)sender;
GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
int count = 0;
btnUpdate.Enabled = false;
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("chkSelect");
if (chk.Checked)
{
count++;
if(count >=5)
{
btnUpdate.Enabled = true;
btnUpdate.CssClass = "enabledImageButton";
break;
}
}
}
}
You are counting the number of checkboxes with you count, not the number of checked checkboxes
As such you code is always going to 'fire' if the 5th checkbox is checked, irrespective of the checkstates of the previous 4.
Looks like you want:
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("chkSelect");
if (chk.Checked) count++;
if (count >= 5)
{
btnUpdate.Enabled = true;
btnUpdate.CssClass = "enabledImageButton";
break; //Avoid hitting more rows than necessary
}
}
this means that you will only Enable if at least 5 are checked, as other posters have said you have nothing to disable it so it will stay enabled.
I have added dynamic tabs which have dynamic datagrid having DataGridCheckBoxColumn and also a dynamic checkBox on the tab which triggers selects all for datagrid checkboxes.
I am trying to implement something along these line.
private void cbSelectAll_CheckedChanged(object sender, EventArgs e)
{
if (cbSelectAll.Checked)
{
foreach (DataGridViewRow row in relatedPatientsDG.Rows)
{
row.Cells[0].Value = true;
}
}
else
{
foreach (DataGridViewRow row in relatedPatientsDG.Rows)
{
row.Cells[0].Value = false;
}
}
}
But this method is to be dynamic as well which needs to verify which tab/datagrid DataGridCheckBoxColumn was selected because I am creating everything dynamically on the tab.
As an example if I have a dataGrid called relatedDG have DataGridColumnCheckBox then the events method to trigger select all and unselect all would be like. I need to make a similar change but for dynamic datagridcheckbox so nothing is hardcoded.
private void cbSelectAllSameVisits_CheckedChanged(object sender, EventArgs e)
{
if (cbSelectAllSameVisits.Checked)
{
foreach (DataGridViewRow row in relatedDG.Rows)
{
row.Cells[0].Value = true;
}
}
else
{
foreach (DataGridViewRow row in relatedDG.Rows)
{
row.Cells[0].Value = false;
}
}
}
You can make use of the fact that each pair of a grid and a checkbox is on the same tabpage - they are both children of the same container control.
This allows you to have a single method that you attach to the CheckedChanged event of all the checkboxes on creation:
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = sender as CheckBox;
DataGridView dg = cb.Parent.Controls.Cast<Control>()
.Where(c => c.GetType() == typeof(DataGridView))
.FirstOrDefault() as DataGridView;
if (dg != null)
{
if (cb.Checked)
{
foreach (DataGridViewRow row in dg.Rows)
{
row.Cells[0].Value = true;
}
}
else
{
foreach (DataGridViewRow row in dg.Rows)
{
row.Cells[0].Value = false;
}
}
}
}
That code uses the sender parameter of the event handler to identify the checkbox that was clicked then searches through the controls belonging that that checkboxes parent for the first DataGridView.
I want to get the value of a CheckBox in a GridView. I'm able to bind the GridView using the event RowDataBound. But on a Click event of a Button the value of the CheckBox is always false even if I checked the CheckBox.
Code Behind:
protected void GridView1_RowDatabound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList dddate = (DropDownList)e.Row.FindControl("DdlDate");
DropDownList ddYear = (DropDownList)e.Row.FindControl("DdlYear");
for (int i = System.DateTime.Now.Year; i > (System.DateTime.Now.Year) - 100; i--)
ddYear.Items.Add(Convert.ToString(i));
for (int i = 1; i < 32; i++)
dddate.Items.Add(Convert.ToString(i));
}
}
protected void btnRetrieveCheck_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkSel");
if (cb != null && cb.Checked)
{
DropDownList dddate = (DropDownList)row.FindControl("DdlDate"); //Bind data to the GridView control.
DropDownList ddYear = (DropDownList)row.FindControl("DdlYear");
DropDownList ddmonth = (DropDownList)row.FindControl("DdlMonth");
}
}
}
You should hook up to the Checked event on the checkbox instead of the click event.
Your question is not very clear. Can you post some more of the code. For example, what is btnRetrieveCheck - is it the actual checkbox? Please update the question with your XAML so we can see what binding is in place.
I am using checkbox in gridview .... I am using it in 1st cell.... When I select the checkbox at run time, I need to get those values... but on selecting or on click to checkbox, it's not finding or value is taking as FALSE... how to write in asp.net backend and in c# code?
<asp:TemplateField>
<ItemTemplate >
<asp:checkbox id="ShowAddress" runat="server" />
</ItemTemplate>
</asp:TemplateField>
Code-behind:
protected void Button1_Click(object sender, EventArgs e)
{
// Looping through all the rows in the GridView
foreach (GridViewRow di in GridView1.Rows)
{
CheckBox chkBx = (CheckBox)di.FindControl("ShowAddress");
if (chkBx != null && chkBx.Checked)
{
/// put your code here
}
}
}
Is there any implementation to be done in script at page load?
Can anyone help?
How do you populate your GridView? If you do this in Page_Load, make sure you are not doing it on postbacks (check IsPostBack).
Is your chkBx variable null?
The following code works:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = row.Cells[0].Controls[0] as CheckBox;
if (chk != null && chk.Checked)
{
// ...
}
}
}
StringCollection idCollection = new StringCollection();
string strID = string.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkDelete = (CheckBox) GridView1.Rows.Cells[0].FindControl("chkSelect");
if (chkDelete != null)
{
if (chkDelete.Checked)
{
strID = GridView1.Rows.Cells[1].Text;
idCollection.Add(strID);
}
}
}
for more details check this link: http://www.itworld2.com/ghowto.aspx?id=69
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Loadgridview();// its a correct
}// not Loadgridview() here if you load above error is occur
}
check it
int i = 0;
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = (CheckBox)GridView_AdminTags.Rows[i].Cells[0].FindControl("chkTag");
if (chk != null)
if (chk.Checked)
{
////.......;
}
i++;
}
i = 0;
Jakob Answer will work if below line is used. Even one control only in the cell, index need to be 1 not 0
CheckBox chk = row.Cells[0].Controls[1] as CheckBox;
Thank you
Sam