0

I have a grid like below.

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

In Code Behind:-

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

Now the message shows - RegId =.

I can't get the value. So I change the RedId BoundField as vissible. Now I got the Value.

that is RegId =6.

I have two issue now -

1) How to get the RegId value in Non Visible Column. 2) How I find the Row value which i click... bzs the only i can change the ROWVALUE in code..

 string text = GridView1.Rows[ROWVALUE].Cells[5].Text;
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sagotharan
  • 2,586
  • 16
  • 73
  • 117

1 Answers1

0

That is certainly not the right way to do it. You might want to look at using DataKeys to achieve this. With current approach, when you add a new column to your grid your code will fail.

  1. Add your RegId column inside DataKeys property on your gridview
  2. Reference your gridview datakey for the current row in codebehind like this

     int regId= Convert.ToInt32(YourGridview.DataKeys[rowIndex]["RegId"].ToString());
    
Zo Has
  • 12,599
  • 22
  • 87
  • 149
  • or use Templatefields, Row Command, FindControl. Sample: https://yasserzaid.wordpress.com/2011/11/13/find-control-in-gridview-rowcommand-event/ http://stackoverflow.com/questions/1665066/gridview-template-how-to-grab-data-from-selected-row – CodingSlayer Jul 24 '12 at 18:17