How to find the value of particular row in gridview?
Solution 1
Use the following:
CustomersGridView.Rows[rowIndex].Cells[cellIndex].Text
More about .Rows collection
If you are interested in FindControl
method, the most suitable event for you is RowDataBound:
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var control = e.Row.Cell[cellIndex].FindControl("ControlID");
e.Row.Cells[1].Text = ((TypeOfControl)control).Text;
}
}
Solution 2
Assuming one cell per column this should do the trick:
string cellValue = GridView1.Rows[4].Cells[1].Text;
That would give you the value of column 2 at row 5.
Depending on how the row is built (if, for example, one column has a DropDownList or some other such control) you might be better off doing FindControl(controlID)
.
UPDATE
Say you have a DropDownList
in the 2nd column with ID="ddlMyDropDown":
DropDownList ddl = (DropDownList)GridView1.Rows[4].FindControl("ddlMyDropDown");
Which event to put the code in depends on why you're trying to find the value. If you can give an example of when/why you'd need to get the value, we can tell you which event it should probably go in.
UPDATE 2
Assuming there's a button in the row, handle the RowCommand
event:
protected void GridVie1_RowCommand(object sender, GridViewCommandEventArgs e)
{
rowIndex = Convert.ToInt32(e.CommandArgument);
DropDownList ddl = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("ddlMyDropDown");
// Do something
}
Do be cautious here, however, as the RowCommand is fired whenever a button in the row is clicked, so you'd need to make sure which button was clicked. You could use e.CommandName
to get the CommandName of the button if you set it, but be aware that there are some predefined names.
See GridView.RowCommand Event for more information.
If you can update your question with some more information (i.e., why you need to get the value and what interaction with the GridView would start that process, like a button click) a more precise answer can be provided.
I'm about done for the night, but I'll check back on this thread tomorrow.
Solution 3
If you know the value of a cell then we can find the row
protected void GridView1_DataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text.Contains("sometext"))
{
//code here..
}
}
}
xorpower
Reached 500 Repo on May 22'11 Reached 600 Repo on Jul 29'11 Reached 700 Repo on Aug 10'11 Reached 800 Repo on Sep 09'11 Reached 900 Repo on Oct 13'11 Reached (& crossed) 1000 Repo during Mar 14-19'12 Reached 1300 Repo on May 8 2013
Updated on November 18, 2020Comments
-
xorpower over 3 years
How can i find value of particular row in gridview. Say a gridview has 10 rows and 4 columns. I know that we can find the row number from row index, but how can we find what is the value of column 2 of row 5 (as per my gridview) or know a value of the cell?
-
xorpower over 12 yearsHow to do with FindControl? Plus, where to put the code, i mean which event?
-
xorpower over 12 yearsfrom above example(ddl), i am not getting which row user has clicked. I cannot give as command argument as i have alerady provided ID (primary key column) as command argument. Hence either i need to know which row is clicked or else if i am giving row index as command argument, i need to know the value of ID (primary key column)
-
Tim over 12 yearsBy row clicked, if you mean a button in the row was clicked, you could handle the RowCommand event. Is that what you're doing?
-
xorpower over 12 yearsYes.. something like that... either i need to know the which row was clicked or the value of ID..
-
xorpower over 12 yearswhat needs to be given in cellIndex?
-
xorpower over 12 yearsthanks!. but rowcommand event is not being fired up? I h've binded with gridview still..
-
VMAtm over 12 years@Xor power You must cast your control to the needed type. Updated the Answer.