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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving a string value from a datagrid field

Status
Not open for further replies.

Linebacker

Programmer
Mar 14, 2002
8
DE
Hello,

I want to read the value of a string which is in a field in a DataGrid. The primary key of the row I want to read from is chosen by a randomizer.

I have already stored the result of the randomizer in a variable. (see source code below)

Looking forward to your answers.


Random rnd = new Random();
int Line = rnd.Next(MaxValue)+ 1;
this.lunch1.DataSetLunch.Rows.Find(Line);


LB
 
this.lunch1.DataSetLunch.Rows.Find()requires an object or object[] as parameters.
So, if the DataTable has a primary key column which is an integer then you do:
Random rnd = new Random();
int Line = rnd.Next(MaxValue)+ 1;
object oLine = new object;
oLine = Line;
DataRow dr = this.lunch1.DataSetLunch.Rows.Find(oLine); // Finds the row with the primary key equal to oLine or null if not found
if (dr !=null)
dr["columnName"].Tostring();
or
dr[1].ToString(); // get the string value of column 1

If there are multiple columns in the primary key then:
DataTable dt;
object [] oKeys = new object[2]; // two columns in the primary key
oKeys[0]="John";
oKeys[1]="Gregory";
DataRow dr = dt.Rows.Find(oKeys);
....


-obislavu-
 
Thank you very much.

Worked out fine. Really helped me.


LB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top