C# selectedrow on hidden boundfield

C# selectedrow on hidden boundfield,c#,asp.net,gridview,C#,Asp.net,Gridview,我已经在这里完成了研究,现在我可以在gridview中成功地获得隐藏的boundfield列的值。问题是我无法将目标设置为所选行的隐藏列 我可以使用GridView1.SelectedRow.Cells[7]文本隐藏列上的类似内容。可能吗 这是我的密码: protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { //GridView1.Columns[7].Visible

我已经在这里完成了研究,现在我可以在gridview中成功地获得隐藏的boundfield列的值。问题是我无法将目标设置为所选行的隐藏列

我可以使用
GridView1.SelectedRow.Cells[7]文本隐藏列上的类似内容。可能吗

这是我的密码:

   protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //GridView1.Columns[7].Visible = true;
        //stock_id_gridview_1();
        foreach (GridViewRow row in GridView1.Rows)
        {
            string id = GridView1.DataKeys[row.RowIndex]["id"].ToString();

            con.Open();
            cmd = new SqlCommand(@"SELECT transaction_id,transaction_number 
                               FROM stocks_history
                               WHERE id = @id", con);

            cmd.Parameters.AddWithValue("@id", id);

            rdr = cmd.ExecuteReader();

            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                    transaction_id = rdr["transaction_id"].ToString();

                    //lbl_test_id.Text = rdr["transaction_id"].ToString();
                    transaction_number = rdr["transaction_number"].ToString();

                    lbl_test_id.Text = transaction_id + " " + transaction_number;
                }
            }

            con.Close();
        }
以下是我的gridview:

<asp:GridView ID="GridView1"
                        CssClass="table table-hover table-striped"
                        runat="server"
                        AutoGenerateColumns="False" DataKeyNames="id" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                        <Columns>
                            <asp:BoundField DataField="productName" HeaderText="Product Name"
                                SortExpression="DateField" />
                            <asp:BoundField DataField="stock_name" HeaderText="Stock Name"
                                SortExpression="DateField" />
                            <asp:BoundField DataField="stock_id" HeaderText="Stock I.D."
                                SortExpression="DateField" />
                            <asp:BoundField DataField="stock_in" HeaderText="Stock In"
                                SortExpression="DateField" />
                            <asp:BoundField DataField="stock_out" HeaderText="Stock Out"
                                SortExpression="DateField" />
                            <asp:BoundField DataField="stock_on_hand" HeaderText="On hand"
                                SortExpression="DateField" />
                            <asp:BoundField DataField="max_date" HeaderText="Date & Time"
                                DataFormatString="{0:MM-dd-yyyy hh:mm tt}"
                                SortExpression="DateField" />
                            <asp:BoundField DataField="id" Visible="false" HeaderText="id"
                                SortExpression="DateField" />
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:HiddenField ID="HiddenField1" runat="server"
                                        Value='<%# Eval("id") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:ButtonField Text="Select" ControlStyle-CssClass="btn btn-info btn-sm" CommandName="Select" ItemStyle-Width="150" HeaderText="Review">
                                <ControlStyle CssClass="btn btn-info btn-sm"></ControlStyle>

                                <ItemStyle Width="150px"></ItemStyle>
                            </asp:ButtonField>
                        </Columns>
                    </asp:GridView>

我想针对这个绑定字段:

    <asp:BoundField DataField="id" Visible="false" HeaderText="id"
                                SortExpression="DateField" />

如果使用属性
DataKeyNames
,则可以从不可见列中获取值,如

使用属性

   <asp:GridView runat="server" ID="GridView" DataKeyNames="id">
    </asp:GridView>
获取选定行隐藏列值的步骤

var id = GridView.DataKeys[GridView.SelectedRow.RowIndex].Values[0];

如果使用属性
DataKeyNames
,则可以从不可见列中获取值,如

使用属性

   <asp:GridView runat="server" ID="GridView" DataKeyNames="id">
    </asp:GridView>
获取选定行隐藏列值的步骤

var id = GridView.DataKeys[GridView.SelectedRow.RowIndex].Values[0];

您的意思是“无法将目标设置为选定行的隐藏列”。我正在考虑使用
GridView1.SelectedRow.Cells[7].Text隐藏列上的类似内容。可能吗?可以,但需要使用DataKeyNames属性。你也可以分享你的html代码吗?@mww是的,我想我做了数据键的事情。我不太明白。我要更新我的问题。你的意思是“不能针对我选择的行的隐藏列”。我想使用
GridView1.SelectedRow.Cells[7]。Text隐藏列上的类似内容。可能吗?可以,但需要使用DataKeyNames属性。你也可以分享你的html代码吗?@mww是的,我想我做了数据键的事情。我不太明白。我要更新我的问题。我选择的行的隐藏值?抱歉,但你的代码和我的代码工作方式相同。我需要在隐藏列上选择显示功能是的,你的列“id”其中索引7是不可见的,所以您必须添加属性并获取它,如我所示,仅此而已。案例中的RowIndex将为row.RowIndex和KeyIndex将为0,因为您使用的是一个属性DataKeyNames。如果我选择了一个特定行的隐藏列,我如何使用它?它说
不包含CurrentCell的定义
我选择的行的隐藏值?抱歉,但您的代码与我的代码的工作方式相同。我需要在隐藏列上选择显示功能是的,您的列“id”其中索引7是不可见的,所以您必须添加属性并获取它,如我所示,仅此而已。在您的案例中,RowIndex将是row。RowIndex和KeyIndex将是0,因为您使用的是一个属性DataKeyName。如果我选择了特定行的隐藏列,如何使用它?它说
不包含CurrentCell的定义