This is a .aspx page.
I currently have this:
foreach (GridViewRow gvr in gvShows.Rows)
{
if (gvr.RowType == DataControlRowType.DataRow)
{
if (((CheckBox) gvr.FindControl("cbSelect")).Checked)
{
string dataSource = gvr.Cells[1].Text;
}
}
}
Which works if the column is visible.
However, the gridView column is now hidden but I still need to capture its value.
How would I go about converting the above code to get the dataSource value which is in a hidden column?
Related
I have a gridview and on some condition basis, I am inserting text box in it in RowDataBound:
private void GetColumnWithValidation(GridViewRowEventArgs e, string columnName, int columnLength)
{
if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, columnName)).Length > columnLength)
{
int colindex = GetColumnIndexByName(e.Row, columnName);
TextBox txt = new TextBox();
txt.Text = Convert.ToString(DataBinder.Eval(e.Row.DataItem, columnName));
txt.BorderColor = Color.Red;
txt.ID = "txt_" + i + "_" + colindex;
lstErrorTracker.Add(i + "_" + colindex);
//link.NavigateUrl = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "PV_INVOICE_LOCATION"));
e.Row.Cells[GetColumnIndexByName(e.Row, columnName)].Controls.Add(txt);
}
}
Now if I change some data in text box of gridview I want that to be fetch whenever I click Update button. Update button is placed outside of gridview as a normal asp.net button.
But when I am trying to fetch the data I am getting null.
foreach (GridViewRow row in GVUploadDetails.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
TextBox txt= (row.Cells[3].FindControl("txt_0_3") as TextBox);
var test = txt.Text;
}
}
I am getting text data as null. Please help how to get those values. And on other hand I want the entire data of the gridview as a Dataset or datatable how can I get that.
The NamingContainer is the GridViewRow not the cell, so you should use:
foreach (GridViewRow row in GVUploadDetails.Rows)
{
TextBox txt = row.FindControl("txt_0_3") as TextBox;
// ...
}
If this doesn't fix the issue, where in the page's life-cycle do you addd this TextBox programatically? You know that you always have to re-create it on every consecutive postback? Do that latest in Page_Load, better Page_Init.
If you add it from RowDataBound you have to re-create it in RowCreated, otherwise it's too late to retain the ViewState.
In the last column of my asp .net GridView I have an image and a checkbox. I want to check if that checkbox is checked, for all rows. I am getting null reference exception at if cause maybe chk is null. any help?
try
{
foreach (GridViewRow row in grdSurveyDetails.Rows)
{
int index = row.Cells.Count - 1;
CheckBox chk = row.Cells[index].Controls[1] as CheckBox;
int rowIndex = row.RowIndex;
if (chk.Checked)
{
ClientScript.RegisterClientScriptBlock(typeof(Page), "alert", "<script>alert("+rowIndex+" is checked!);</script>");
}
}
}
catch (Exception)
{
throw;
}
I'm assuming that you populate the gridview via code, so that you always have an image and checkbox in that last cell. If that's the case, something that come to my mind is that you may be accessing the header row which does not have the checkbox in there.
Nonetheless, could you try doing the null check in there, something like
if (chk?.Checked)
That ? is doing a null check for you. If it's null, then do not do anything with that row.
NB: if the cell doesn't always contains image and checkbox, then use caution as you may have a checkbox in that cell which wasn't at index 1 (ie: you may consider using #Rojalin Sahoo approach to search for checkbox explicitly)
You can check all checked like this
Lets say you have
Aspx
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
Then in
Code Behind
foreach (GridViewRow row in grdSurveyDetails.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
int index = row.Cells.Count - 1;
CheckBox chkRow = (row.Cells[index].FindControl("chkRow") as CheckBox);
if (chkRow.Checked)
{
//do something
}
}
}
Please look at this complete example
Here
I know this type of question is asked before but no one got the answer yet...!!
How to get a Grid View Row from Data Keys.I don't want to iterate through the whole gird view.
I want to access specific text box(s) in a grid view.
for example in a 100 rows grid view i only want to disable any 2 text boxes on Page Load.
I have Data Key Names defined in grid, but how to get rows from it ?
any idea?
Please try following code..
protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Get data row view
DataRowView drview = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find textbox control
TextBox txtname = (TextBox)e.Row.FindControl("txtName");
string Name = txtname.Text;
if (((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString() == "Leave")
{
txtname.disable=true;
}
else
{
txtname.disable = false;
}
}
}
I have a GridView which has multiple rows, on each row I have a CheckBox and a HiddenField. On button click I want to check if the CheckBox is checked and if it is I want to take the value of the HiddenField for that row. Each HiddenField on each row has a different value. User could check multiple CheckBoxes so I need to be able to pull the value of each HiddenField.
Any help will be really appreciate it.
Thank you
Loop through each row in the grid, check if the checkbox is checked and if it is, grab the value of the hidden field.
foreach (GridViewRow row in grdView.Rows)
{
if((row.FindControl("chkBoxId") as CheckBox).Checked)
{
string hiddenFieldValue = (row.FindControl("hiddenFieldId") as HiddenField).Value;
}
}
Where chkBoxId is the ID property of your checkbox on the page and hiddenFieldId is the ID of the hiddenfield control on your page.
Possible duplicates.
How to get values of CheckBoxes inside a gridview that are checked using asp .net
Get the id of selected checkboxes in gridview (Asp.net) c#
How to get the value in the gridview which the checkbox is checked?
One of the answer in above links :
foreach(Gridviewrow gvr in Gridview1.Rows)
{
if(((CheckBox)gvr.findcontrol("CheckBox1")).Checked == true)
{
//Get hidden field value here.
}
}
You can use a code like this:
protected void BtnMybutton_click( Object sender, EventArgs e)
{
Button Mybutton = (Button) sender;
GridViewRow row = (GridViewRow) MyButton.NamingContainer;
CheckBox ChkTest = (CheckBox) row.FindControl("ChkTest");
HidenFiekd HdfValue = (HidenField) row.FindControl("HdfValue");
if(ChkTest.Checked)
{
Console.WriteLine(HdfValues.Value);
}
}
I am new in ASP.NET ,I would have editable gridview in asp.net using C# , I found this editable gridview (Database , Project) in codeproject but i didn't realize how
its work specially this part of code:`
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList dp= (DropDownList )e.Row .FindControl ("DropDownList1");
DataTable dt = load_department();
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem lt = new ListItem();
lt.Text = dt.Rows[i][0].ToString();
dp.Items.Add(lt);
}
dp.SelectedValue = drv[3].ToString();
RadioButtonList rbtnl = (RadioButtonList)e.Row.FindControl("RadioButtonList1");
rbtnl.SelectedValue = drv[5].ToString();
CheckBoxList chkb = (CheckBoxList)e.Row.FindControl("CheckBoxList2");
chkb.SelectedValue = drv[6].ToString();
}
}
}
`
Why she/he do this??
RowDataBound event fires when you bind the grid to a datasource, say, for example, a datatable.
For each row in the datatable, this code will run, and will, depending on the values in that row, put a value in each column of the grid view.
FindControl is used to find the control with the spcific name on that line of the grid view - remember, it will be repeated many times for as many rows as you have.
Once the control has been found, the value is set.
You are effectively setting up each row of the grid view for each row of the data in your data source.
Take a look at http://msdn.microsoft.com/en-us/magazine/cc163933.aspx for an overview of the intent behind this control.
** RowDataBound Occurs when a data row is bound to data in a GridView control.
**DataControlRowState Specifies the state of a row in a data control for eg.Edit,Insert,Selected etc
** RowState Gets the current state of the row with regard to its relationship to the DataRowCollection.
now in that if condition your dropdownbox (DropDownList1) is filled and RadioButton and Checkebox are setting up their selected values.