How to get the value of checkbox in gridview using C#? - c#

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.

Related

unable to find edit template control checkbox in row command

I tried to get the control of edit template which is checkbox in row command event but I am unable to get it, but I am getting label control which is in row index.
I tried the above below code to get the control:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow gvr = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
Label icllbl = (Label)GridView1.Rows[gvr.RowIndex].FindControl("icllbl");
CheckBox iclcb = (CheckBox)GridView1.Rows[gvr.RowIndex].FindControl("iclcb");
if (e.CommandName.Equals("Edit"))
{
if (icllbl.Text == "Y")
{
iclcb.Checked = true;
}
}
}
And I tried RowDataBound event also luckily I am getting checkbox control here but this time I am unable to get the Label control in below code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label icllbl = (Label)e.Row.FindControl("icllbl");
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
CheckBox iclcb = (CheckBox)e.Row.FindControl("iclcb");
if (icllbl.Text == "Y")
{
iclcb.Checked = true;
}
}
}
}
Please correct me if I am wrong anywhere.
Thanks in advance!
In your RowCommand event use control class to cast into GridViewRow:
GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
int rowIndex = row.RowIndex;
And in RowDataBound event place Label control inside Edit (check for EditTemplate) check:
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
Label icllbl = (Label)e.Row.FindControl("icllbl");
CheckBox iclcb = (CheckBox)e.Row.FindControl("iclcb");
//... other code of lines will be here
}

how to prevent dynamically added textbox in gridview to get dispose after postback

I am able to add unlimited textbox column in a row of gridview. but after postback these textboxes get disposed. So how to retain these textboxes and their values after postback?
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i = 3;
if (e.Row.RowType == DataControlRowType.DataRow)
{
crcl = (List<string>)ViewState["bdi2"];
foreach(string a in crcl)
{
TextBox TextBox101 = new TextBox();
TextBox101.ID=a;
TextBox101.Width = 60;
TextBox101.Text = (e.Row.DataItem as DataRowView).Row[a].ToString();
e.Row.Cells[i].Controls.Add(TextBox101);
//TextBox101.AutoPostBack = true;
i++;
}
}
}
I hope that this link is useful to you for adding textbox in gridview
http://www.aspforums.net/Threads/201270/Dynamically-add-TextBox-control-to-GridView-Row-in-ASPNet/
And this link too.
http://www.aspsnippets.com/Articles/Adding-Dynamic-Rows-in-ASP.Net-GridView-Control-with-TextBoxes.aspx

How to pass along cell value to row databound event after rowediting event

Below is my code behind:
public void enableEditMedChange(object sender, GridViewEditEventArgs e)
{
MedChangeTable.EditIndex = e.NewEditIndex;
}
protected void MedChangeTable_RowDataBound(object sender, GridViewRowEventArgs e)
{
using (DeveloprodDataClassDataContext adminDB = new DeveloprodDataClassDataContext())
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList newMedChangeChangeDD = (DropDownList)e.Row.FindControl("NewMedChangeChangeDD");
newMedChangeChangeDD.SelectedValue = ???
}
}
}
}
I would like to set the drop down's selected value to whatever value was in one of the cells in the row who's edit button was clicked on. How can I pass along this value to the row data bound event?
You can get the value like this:
DropDownList newMedChangeChangeDD = (DropDownList)e.Row.FindControl("NewMedChangeChangeDD");
newMedChangeChangeDD.SelectedValue = DataBinder.Eval(e.Row.DataItem, "YourDataFieldName").ToString();
More informations here.

How to bind tooltip on datagrid rows?

I'm trying to bind datagrid rows with tooltip. In which event should i write the code? The row created event doesnt hold my data to bind and returning blank. The code reference is given below:
protected void gdvActionItemView_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[2].ToolTip = e.Row.Cells[2].Text;
if (e.Row.Cells[2].Text.Length > 100)
{
e.Row.Cells[2].Text.Substring(0, 100);
}
}
Please help.
You can right Row Databound event such as:
protected void grdUserClone_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int colIndex = 0; colIndex < e.Row.Cells.Count; colIndex++)
{
e.Row.Cells[colIndex].Attributes.Add("title", e.Row.Cells[colIndex].Text);
}
}
}
Found the answer. It should be written under RowDataBound.

Postback problem with checkbox (within gridview) on button click event

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

Categories