繁体   English   中英

获取 GridView 行的单元格值

[英]Get the cell value of a GridView row

我正在使用 GridView - AutoGenerateSelectButton = "True"到 select 行以获得第 1 列单元格值。

我试过了:

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

它会写入“Cell Value”,但不会写入其他任何内容。

我终于能够获得行数(索引)而不是单元格值。

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

我试过了:

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

并且仍然返回索引行。

还有其他建议吗? 谢谢!

尝试将代码更改为

// Get the currently selected row using the SelectedRow property.
GridViewRow row = dgCustomer.SelectedRow;

// And you respective cell's value
TextBox1.Text = row.Cells[1].Text

更新:(基于我的评论)如果您要获取的所有内容都是所选行的主键值,那么另一种方法是设置

datakeynames="yourprimarykey"

对于gridview定义,可以从后面的代码访问,如下所示。

TextBox1.Text = CustomersGridView.SelectedValue.ToString();

Windows窗体迭代技术

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
            }
        }
    }
}

我建议你在模板字段里面使用HiddenField,使用FindControl来查找这个字段。

即:

ASPX

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

代码背后

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)
        {
           ..
        }
    }

你试过Cell[0]吗? 记住索引从0开始,而不是1。

扩展丹尼斯R上面的回答...这将根据标题文本得到值(所以你不需要知道什么列...特别是如果它的动态变化)。

在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;
    }
}

我和你的问题一样。 我发现当我在GridView中使用BoundField标签来显示我的数据时。 row.Cells[1].Text正在用于:

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

但是当我使用TemplateField标签来显示这样的数据时:

 <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只返回null。 我在这个问题上遇到了很长时间。 我最近想出来,我想与遇到同样问题的人分享我的解决方案。 请随时编辑这篇文章和/或纠正我。

我的解决方案

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

我使用Controls属性来查找我用来显示数据的Label控件,你可以找到你的。 当您找到它并转换为正确的类型对象时,您可以提取文本等。 例如:

string showText = lbCod.Text;

参考: 参考

我在命名列中寻找一个整数值,所以我做了以下内容:

int index = dgv_myDataGridView.CurrentCell.RowIndex;

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

关于这一点的好处是列可以位于网格视图中的任何位置,您仍然可以获得该值。

干杯

SelectedIndexChanged事件中,来自选定行/单元格的值可以如下所示找到:

string id = gvLR.SelectedRow.Cells[3].Text.FromCellNumber();

在隐藏单元格的RowCommand事件值中,可以找到如下所示:

HiddenField hdnProgramId = (((e.CommandSource as 
LinkButton).Parent.FindControl("SystemId")) as HiddenField);
string id = hdnProgramId.Value;

SelectedRowDelete事件中,任何单元格的值都可以按如下方式获取:

string _fileEntryId = gvLR.SelectedRow.Cells[1].Text;

对于 Gridview 中的任何/所有行,如下所示:

foreach (GridViewRow row in gv.Rows) 
{
  if (row.RowType == DataControlRowType.DataRow)
   { 
   string _FileEntryId = ((HiddenField)row.FindControl("SystemId")).Value;
   string kk=row.Cells[3].Text; 
   } 
}
string id;
foreach (GridViewRow rows in grd.Rows)
{
   TextBox lblStrucID = (TextBox)rows.FindControl("grdtext");
   id=lblStrucID.Text
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM