如何在ASP.net中获取不可见的gridview单元格值?

时间:2012-07-24 05:10:19

标签: asp.net gridview

我的网格如下。

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" s                                      
                                    onrowcommand="GridView1_RowCommand">
  <Columns>
      <asp:ButtonField DataTextField="Name" HeaderText="Name" />
      <asp:BoundField DataField="ArrDate" DataFormatString="{0:MM/dd/yyyy}" 
                              HeaderText="Arr Date" />
      <asp:BoundField HeaderText="Dep Date" DataField="DepDate" 
                           DataFormatString="{0:MM/dd/yyyy}" />
      <asp:BoundField HeaderText="Mail" DataField="Mail" />
      <asp:BoundField HeaderText="Status" DataField="Status" />
      <asp:BoundField DataField="ResId" HeaderText="ResId" Visible="False" />                                        
  </Columns>
</asp:GridView>

代码背后: -

try
{
  string text = GridView1.Rows[2].Cells[5].Text;
  ScriptManager.RegisterStartupScript(this, GetType(), "Message", "alert('ResId = " + text  + ".');", true);             
}
catch { }

现在消息显示 - RegId =。

我无法获得价值。所以我将RedId BoundField更改为可变。现在我得到了价值。

即RegId = 6。

我现在有两个问题 -

1)如何在不可见列中获取RegId值。 2)我如何找到我点击的Row值... bzs我唯一能改变代码中的ROWVALUE ..

 string text = GridView1.Rows[ROWVALUE].Cells[5].Text;

1 个答案:

答案 0 :(得分:0)

这当然不是正确的方法。您可能希望使用DataKeys来实现此目的。使用当前方法,当您向网格添加新列时,代码将失败。

  1. 在gridview
  2. 上的DataKeys属性中添加RegId列
  3. 像这样

    引用代码隐藏中当前行的gridview数据键
     int regId= Convert.ToInt32(YourGridview.DataKeys[rowIndex]["RegId"].ToString());