Bryan
Bryan

Reputation: 8697

foreach looping only displaying last value of gridview

I'm binding my gridview with multiple datakeys. I'm trying to let these datakey value to be displayed on the textbox when a particular row of gridview is being selected. Therefore i tried the following method below.

I managed to store my database value into a string based on the column number. I'm using a foreach so as to loop the entire gridview when a particular row is being selected. But i'm not very sure why does it only display the last value of my gridview ?

protected void GWCase_SelectedIndexChanged(object sender, EventArgs e)
    {

        foreach (GridViewRow gvr in GWCase.Rows)
        {
            string detail = GWCase.DataKeys[gvr.RowIndex].Values[0].ToString();
            string propertydetail = GWCase.DataKeys[gvr.RowIndex].Values[1].ToString();
            string suspectdetail = GWCase.DataKeys[gvr.RowIndex].Values[2].ToString();

            tbdetails.Text = detail;
            tbproperty.Text = propertydetail;
            tbsuspect.Text = suspectdetail;
        }

I cant really convert it into a proper for loop because i got the gridview row there as well. Basically, i'm trying to display out the different value of the different datakey based on the selected gridview row.

Upvotes: 2

Views: 2388

Answers (3)

Bryan
Bryan

Reputation: 8697

Managed to figure out the answer.

protected void GWCase_SelectedIndexChanged(object sender, EventArgs e)
{
  int idx = GWCase.SelectedIndex; //add this line in
  string detail = GWCase.DataKeys[idx].Values[0].ToString();
  string propertydetail = GWCase.DataKeys[idx].Values[1].ToString();
  string suspectdetail = GWCase.DataKeys[idx].Values[2].ToString();

  tbdetails.Text = detail;
  tbproperty.Text = propertydetail;
  tbsuspect.Text = suspectdetail;
}

Upvotes: 2

R.C
R.C

Reputation: 10565

Answer to your question: Basically, i'm trying to display out the different value of the different datakey based on the selected gridview row.

To get the values from the selected row, Use DataKeyNames property of GridView. Make sure you have a selectbutton present using AutoGenerateSelectButton="true" OR adding a button and set its CommandName Property to "Select".

Set DataKeyNames property to the Primary Key of your table. This can also contain more than one fields.

<asp:GridView ID="Employees" runat="server" DataSourceID="sourceEmployees" 
DataKeyNames="EmployeeID">

In your selectedIndexChanged event, you can access the Value of this PrimaryKey column as well as other columns:

protected void gridEmployees_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    int index = gridEmployees.SelectedIndex;

//retrieve the Primary Key field from the SelectedDataKey property. 
  int ID = (int)gridEmployees.SelectedDataKey.Values["EmployeeID"];

// Get other columns values 
string firstName = gridEmployees.SelectedRow.Cells[2].Text; 
string lastName = gridEmployees.SelectedRow.Cells[1].Text; 
lblRegion.Text = gridEmployees.SelectedRow.Cells[9].Text;

}

One another way you can directly determine the data key value of the first key field of the selected row is by using the SelectedValue property

// Returns only the value of First DataKey Field
int ID = (int)gridEmployees.SelectedValue.ToString(); 

Upvotes: 2

TheVillageIdiot
TheVillageIdiot

Reputation: 40507

Well I think you want value from the selected row only. While looping you are not checking if the row is selected or not. That is why it shows only the stuff from last row. Try following:

protected void GWCase_SelectedIndexChanged(object sender, EventArgs e)
{        
    GridViewRow selectedRow = GWCase.SelectedRow;

    if(selectedRow == null) return;

    /*GET VALUES FROM selectedRow AND DISPLAY THEM*/
}

Upvotes: 2

Related Questions