I am using the GridView - AutoGenerateSelectButton = "True"
to select the row in order to get the Column 1 cell value.
I have tried:
GridViewRow row = dgCustomer.SelectedRow;
TextBox1.Text = "Cell Value" + row.Cells[1].Text + "";
And it writes the "Cell Value" but nothing else.
I finally was able to get the number of the row (index) but not the cell value.
GridViewRow row = dgCustomer.SelectedRow;
TextBox1.Text = row.RowIndex.ToString();
I have tried:
TextBox1.Text = dgCustomer.Rows[row.RowIndex].Cells[1].Text;
and still returns the index row.
Any other suggestions? Thank you!
I was looking for an integer value in named column, so I did the below:
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 in any position in the grid view and you will still get the value.
Cheers
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
}
}
}
}
Expanding on Dennis R answer above ... This will get the value based on the Heading Text (so you don't need to know what column...especially if its dynamic changing).
Example setting a session variable on 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;
}
}
I had the same problem as yours. I found that when i use the BoundField
tag in GridView to show my data. The row.Cells[1].Text
is working in:
GridViewRow row = dgCustomer.SelectedRow;
TextBox1.Text = "Cell Value" + row.Cells[1].Text + "";
But when i use TemplateField
tag to show data like this:
<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>
The row.Cells[1].Text
just return null. I got stuck in this problem for a long time. I figur out recently and i want to share with someone who have the same problem my solution. Please feel free to edit this post and/or correct me.
My Solution:
Label lbCod = GridView1.Rows["AnyValidIndex"].Cells["AnyValidIndex"].Controls["AnyValidIndex"] as Label;
I use Controls
attribute to find the Label
control which i use to show data, and you can find yours. When you find it and convert to the correct type object than you can extract text and so on. Ex:
string showText = lbCod.Text;
Reference: reference
I suggest you use a HiddenField inside template field use FindControl to find this field.
ie:
ASPX
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hfFname" runat="server" Value='<%# Eval("FileName") %>' />
</ItemTemplate>
</asp:TemplateField>
Code behind
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)
{
..
}
}
Have you tried Cell[0]
? Remember indexes start at 0, not 1.
string id;
foreach (GridViewRow rows in grd.Rows)
{
TextBox lblStrucID = (TextBox)rows.FindControl("grdtext");
id=lblStrucID.Text
}
Source: Stackoverflow.com