-1

I'm new to ASP.NET 4.0. In my project, the data'a are displayed in the Grid View. The user selects a row to edit based on two values which are stored in the Database. My code for calling single value is given here. How can I get datas based on two values from the database. PLease help me with this.

grdViewVacationInfo_RowCommand()
{
            if (e.CommandName == "ViewInfoBtn")
            {
                int index = int.Parse(e.CommandArgument.ToString());
                string key = grdViewVacationInfo.DataKeys[index].Value.ToString();
                Session["ke"] = key.ToString();
            }
}

This is how ill call a single value. but how can i get the more than two values and store it in a Session or any variable.

Thanks In Advance

Amit
  • 21,570
  • 27
  • 74
  • 94

2 Answers2

0

you can try this to fatch grid's column value .

grdViewVacationInfo.Rows[grdViewVacationInfo.SelectedIndex]..Cells[yourcellindex].Text; 
Amit
  • 21,570
  • 27
  • 74
  • 94
0

As you're already using DataKeys you're most of the way there already. DataKeys can hold more than one value - just separate the column names you want with commas:

<asp:gridview runat="server" id="grdViewVacationInfo" 
    DataKeyNames="Column1, Column2" 
... 
</asp:gridview>

Then in your RowCommand event, you access the DataKey values by index:

grdViewVacationInfo_RowCommand() 
{             
    if (e.CommandName == "ViewInfoBtn")
    {                 
        int index = int.Parse(e.CommandArgument.ToString());
        string key1 = grdViewVacationInfo.DataKeys[index].Value[0].ToString();
        string key2 = grdViewVacationInfo.DataKeys[index].Value[1].ToString();
        Session["ke"] = key1;
        Session["ke2"] = key2;
    } 
} 
PhilPursglove
  • 12,511
  • 5
  • 46
  • 68
  • Thank You., but it is not working. It shows an error like "Cannot apply indexing with [] to an expression of type 'object' " – user177777 May 20 '11 at 12:31