Gridview FindControl inside SelectedIndexChanged event - asp.net

I created a TemplateField in my ASP GridView, and now I want to write a small logic for a checkbox in the gridview. I am trying the FindControl code to no success, I've used these combinations...
Dim chkChosen As CheckBox =
'GridView1.Rows(e.RowIndex).FindControl("Checkbox1")
'DirectCast(GridView1.Rows(e.RowIndex).FindControl("Checkbox1"), CheckBox).Value
'chkChosen = (CheckBox)row.FindControl("Checkbox1")
I commented them as I've used a combination of those three to no success. They all give me the same error... "RowIndex is not a Member of SystemArg...". All this is under a SelectedIndexChanged protected sub.

This should work in your case:
Dim chkChosen As CheckBox = CType(GridView1.SelectedRow.FindControl("Checkbox1"), CheckBox)

You Can Get By Gridview Selected Row Index..lyk this
GridViewRow row = GridView1.Rows[GridView1.SelectedIndex];
cHeckbox chk=row.FindControl("chk");

Related

Can't get a checkbox value inside a gridviewrow controls

i programatically populate a gridview with checkbox like this
Dim checkbox As New CheckBox()
checkbox.Checked = True
checkbox.ID = String.Format("chkChecked{0}", i)
MyRow.Cells(i).Controls.Add(checkbox)
later on i tried to access them with this
Dim chk As CheckBox = DirectCast( Row.FindControl(String.Format("chkChecked{0}", i)),CheckBox)
over the row but it does'nt work, any idea of what is going on? thank in advance
I use this code to access CheckBox control inside CellContentClick or CellClick events of the DataGridView :
Dim CheckBox As DataGridViewCheckBoxCell = CType(grdDataGridView.CurrentCell, DataGridViewCheckBoxCell)
If you want you can replace grdDataGridView.CurrentCell with grdDataGridView(columnIndex,rowIndex).
At the end I use EditedFormattedValue to check the value of the CheckBox.
If (CheckBox.EditedFormattedValue) Then
....
End If
Let me know if you need more help.
Regrads,
Boris

Update Label control In first record of DataList during ItemDataBound

Using Asp.net and VB.net. I have a DataList on a webpage. The datalist has a label control. I want to update the text of the label control in the first record with information obtained from the subsequent records as those subsequent records are databound. In other words, each time the datalist gets bound, I want to identify the label in the first record and then update the text of that label. I'm attempting to do this in the ItemDataBound by getting the ClientID of the label in the first record:
Dim strMealPrice As String = CType(e.Item.FindControl("lblMealPrice"), Label).ClientID
and then hold that ClientID in a hidden label outside of the datalist.
If lblhidMealHeaderID.Text = "" then
lblhidMealHeaderID.Text = strMealPrice
End if
Everything works up to this point.
Then each time the datalist ItemDataBound is fired I use findcontrol to try to update the label in the first record but I'm unsure how to format the findcontrol when using a variable for the label control's ClientID (lblhidMealHeaderID.text). But even when I hard code the ClientID of the label in the first record I can't get it to work.
Dim tempLabel As Label = DataList1.FindControl("DataList1_ctl00_lblMealPrice")
or
Dim tempLabel As Label = CType(e.Item.FindControl("DataList1_ctl00_lblMealPrice"), Label)
I get a Object reference not set to an instance of an object. when I try to write to tempLabel.
As you can see I'm grasping here. First, is this the best way to do this - is the ItemDataBound where I should be attempting this? Perhaps you can't update previous records while the DataList is "binding" subsequent records. Second, is ClientID the way to do this - I see ClientID is used mostly for javascript? Third, how do I properly format the FindControl using ClientID?
Any and all help is greatly appreciated.
In ItemDataBound use this
If e.Item.ItemIndex = 0 Then
CType(e.Item.FindControl("lblMealPrice"), Label).Text = strMealPrice
End If
Update
You can find the first label any time after binding by looping through its items.
For Each item as DataGridItem In dgGrid.Items
CType(item.FindControl("lblMealPrice"), Label).Text = strMealPrice
Next

How do I pull out values from a row in a gridview from a onclick event?

I have a datatable that I bind to a gridview to display on screen. I have some buttons in each row of the gridview and when it gets displayed on screen I can click on the button in the row and there is a onclick event being called. In the onclick event I want to pull out the other values from that row so that I can do stuff with them. How would I do that?
e.g. if I select the button in the 3rd row down, I want to pull all the values from the 3rd row.
Here you go:
Dim btnClicked As Button = CType(sender, Button)
Dim gvRow As GridViewRow = CType(btnClicked.NamingContainer, GridViewRow)
Using the gvRow, you can find the other controls in that row to obtain values.
Please mark this as an answer if I answered your question.
Inside your button click event handler use following code:
GridViewRow row = (GridviewRow)((Button)sender).NamingContainer;
TextBox txt = (TextBox)row.FindControl("txtMyText");
Label lbl = (Label)row.FindControl("lblMyLabel");
string txtval = txt.Text;
string lblval = lbl.Text;
// or using cells of the row
string value1 = row.Cells[0].Value;
string value2 = row.Cells[1].Value;

ASP.net Gridview Itemtemplate Dropdownlist

I am using C# ASP.net, and relative new to it, and need to accomplish below.
In my VS2010 web application project I have webform with a Gridview which fetches the data from a table.
In the Grid, I have Commandfiled Button (column1) and Item template with Dropdownlist (column2).
Use case is, user first selects one listitem from 3 list items (H, L and M) and then selects command button.
I am not able to figure out how to extract selected listitem from a selected row
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView2.SelectedRow;
Label4.Text = "You selected " + row.Cells[4].Text + ".";
Label5.Text = "You selected this list item from dropdownlist " + ???? ;
}
Thanks in advance.
The GridViewRow object provides the method FindControl (as do all container controls) to get access to a control in the row by its id. For example, if your DropDownList has an id of MyDropDown, you could use the following to access its selected value:
GridViewRow row = GridView2.SelectedRow;
DropDownList MyDropDown = row.FindControl("MyDropDown") as DropDownList;
string theValue = MyDropDown.SelectedValue;
// now do something with theValue
This is the recommended method for accessing all controls in your GridView. You want to avoid doing things like row.Cells[#] as much as possible, because it easily breaks when you re-arrange or add/remove columns from your GridView.

Getting data from GridView on Button_Click

My problem is:
I have a GridView, which is bound to list of declared DTO objects with column of CheckBoxes. Also, I have a "Save" button on the page. My mission concludes in getting all rows, where CheckBox is checked and get the containing data. Unfortunately, row.DataItem = null on Button1_Click, because, I'm using if(!IsPostBack) gw.DataBind() statement (otherwise, I can't get CheckBox status). Could you give me the best practice for solving my problem?
Thank you in advance!
If you do if(!IsPostBack) --> gw.DataBind(), that will reinitialize your GridView and the checkboxes will be set to unchecked again.
In your case, in Button1_Click event, you can loop through each DataRow on your GridView and find the row which has it's checkbox checked and get all the selected row data.
foreach (GridViewRow gvRow in gw.Rows) {
// Find your checkbox
CheckBox chkBox = (CheckBox)gvRow.FindControl("checkBox");
if (chkBox.Checked) {
// Get your values
string value1 = gvRow.Cells[1].Text;
string value2 = gvRow.Cells[2].Text;
// etc...
}
}

Resources