Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing 3 Values in the same column of a datagrid

Status
Not open for further replies.

ac11nyc

Programmer
Oct 1, 2003
94
US
Ok so i have a datagrid that brings back a few Values. I give the user the option to insert a record anywhere inside the datagrid just as long as the Values are in a correct ascending order. How can i compare the value of the previous row with the value of the selected row as well as comparing it with the row after it?? Any help will greatly be appreciated. Its a simple load of a datagrid so thats why there's no code.
 
To get the previous row and the following row you first need to know the selected row. You can get that in the SelectedIndexChanged event when a row is selected.
Code:
private void myGrid_SelectedIndexChanged(object sender,eventargs e)
{
  int myIndex = myGrid.SelectedIndex;
  // let's say that the values to compare are in cell2
  string myVal = myGrid.Items[myIndex].Cells[2].Text;
  // you can get the previous and next items by adding
  // or subtracting from the value of myIndex
  string myNextVal = myGrid.Items.[myIndex +1].Cells[2].Text;
  // need to check if the first item was selected
  // if so, there is no previous item
  if(myIndex != 0)
     string myPrevVal = myGrid.Items[myIndex -1].Cells[2].Text;
}
And so on...If you are using Template Columns and storing your values in controls (Label, TextBox etc) then you'll first need to cast to the control type and use FindControl:
Code:
Label myLabel = (Label)myGrid.Items[myIndex].Cells[2].FindControl("lblSomeLabel");
// then it's just 
string myVal = myLabel.Text;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top