Get GridView Row Cell Value

I use GridView - AutoGenerateSelectButton = "True" to select a row to get the cell value of column 1.

I tried:

 GridViewRow row = dgCustomer.SelectedRow; TextBox1.Text = "Cell Value" + row.Cells[1].Text + ""; 

And he writes "Cell Value", but nothing more.

Finally, I was able to get the row number (index), but not the cell value.

 GridViewRow row = dgCustomer.SelectedRow; TextBox1.Text = row.RowIndex.ToString(); 

I tried:

 TextBox1.Text = dgCustomer.Rows[row.RowIndex].Cells[1].Text; 

and still returns the index row.

Any other suggestions? Thanks!

+11
c # winforms gridview
source share
8 answers

Try changing your code to

 // Get the currently selected row using the SelectedRow property. GridViewRow row = dgCustomer.SelectedRow; // And you respective cell value TextBox1.Text = row.Cells[1].Text 

UPDATE: (based on my comment) If all you are trying to get is the primary key value for the selected row, then an alternative approach is to set

datakeynames="yourprimarykey"

to define a gridview that can be accessed from code, as shown below.

 TextBox1.Text = CustomersGridView.SelectedValue.ToString(); 
+13
source share

Windows Form Iteration Technology

 foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Selected) { foreach (DataGridViewCell cell in row.Cells) { int index = cell.ColumnIndex; if (index == 0) { value = cell.Value.ToString(); //do what you want with the value } } } } 
+5
source share

I suggest you use the HiddenField field inside the template, using FindControl to find this field.

t

Aspx

  <asp:TemplateField> <ItemTemplate> <asp:HiddenField ID="hfFname" runat="server" Value='<%# Eval("FileName") %>' /> </ItemTemplate> </asp:TemplateField> 

Code for

 protected void gvAttachments_RowDeleting(object sender, GridViewDeleteEventArgs e) { GridView gv1 = (GridView)sender; GridViewRow gvr1 = (GridViewRow)gv1.Rows[e.RowIndex]; //get hidden field value and not directly from the GridviewRow, as that will be null or empty! HiddenField hf1 = (HiddenField)gvr1.FindControl("hfFname"); if (hf1 != null) { .. } } 
+2
source share

Have you tried Cell[0] ? Remember that indexes start at 0, not 1.

+1
source share

More details on Dennis R's answer above ... This will allow you to get a value based on the heading text (so you donโ€™t need to know which column ... especially if it is dynamically changing).

An example of setting a session variable in SelectedIndexChange.

  protected void gvCustomer_SelectedIndexChanged(object sender, EventArgs e) { int iCustomerID = Convert.ToInt32(Library.gvGetVal(gvCustomer, "CustomerID")); Session[SSS.CustomerID] = iCustomerID; } public class Library { public static string gvGetVal(GridView gvGrid, string sHeaderText) { string sRetVal = string.Empty; if (gvGrid.Rows.Count > 0) { if (gvGrid.SelectedRow != null) { GridViewRow row = gvGrid.SelectedRow; int iCol = gvGetColumn(gvGrid, sHeaderText); if (iCol > -1) sRetVal = row.Cells[iCol].Text; } } return sRetVal; } private static int gvGetColumn(GridView gvGrid, string sHeaderText) { int iRetVal = -1; for (int i = 0; i < gvGrid.Columns.Count; i++) { if (gvGrid.Columns[i].HeaderText.ToLower().Trim() == sHeaderText.ToLower().Trim()) { iRetVal = i; } } return iRetVal; } } 
0
source share

I had the same problem as you. I found that when I use the BoundField tag in the GridView to show my data. row.Cells[1].Text works in:

 GridViewRow row = dgCustomer.SelectedRow; TextBox1.Text = "Cell Value" + row.Cells[1].Text + ""; 

But when I use the TemplateField tag to show such data:

  <asp:TemplateField HeaderText="ๆ–™่™Ÿ"> <ItemTemplate> <asp:Label ID="Part_No" runat="server" Text='<%# Eval("Part_No")%>' ></asp:Label> </ItemTemplate> <HeaderStyle CssClass="bhead" /> <ItemStyle CssClass="bbody" /> </asp:TemplateField> 

row.Cells[1].Text just returns null. I am stuck with this problem for a long time. I figure recently, and want to share with someone who has the same problem, my solution. Feel free to edit this post and / or correct me.

My decision:

 Label lbCod = GridView1.Rows["AnyValidIndex"].Cells["AnyValidIndex"].Controls["AnyValidIndex"] as Label; 

I use the Controls attribute to find the Label control that I use to display data, and you can find mine. When you find it and convert it to the correct type object, how can you extract the text and so on. Example:

 string showText = lbCod.Text; 

Link: Link

0
source share

I was looking for an integer value in a named column, so I did the following:

 int index = dgv_myDataGridView.CurrentCell.RowIndex; int id = Convert.ToInt32(dgv_myDataGridView["ID", index].Value) 

The good thing about this is that the column can be at any position in the grid view, and you still get the value.

Hooray

0
source share
 string id; foreach (GridViewRow rows in grd.Rows) { TextBox lblStrucID = (TextBox)rows.FindControl("grdtext"); id=lblStrucID.Text } 
-one
source share

All Articles