DataTable.Select with object reference type

L

Lynn Kear-Noe

I am creating controls at runtime according to
information stored in an SQL CE database. I fill a
dataset with the SQL CE records pertinent to the current
display page, then step through them and create the
controls. Each record describes one control. I want to
store a reference to the newly created control in another
field in that record. This seems to be working. But how
can I look up the record using the object reference?
DataTable.Select requires a select string, and I don't
know how to put the object reference in the select string
properly.

I create the new column like this (dt is type DataTable):
dt.Columns.Add("CtrlRef", System.Type.GetType
("System.Object"));

I store the object reference in the new column like this
(row is type DataRow, aTextBox is type TextBox):
row["CtrlRef"] = (object)aTextBox;

Then in a key event handler, I want to find the sender's
record in the DataTable

private void on_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
//find sender's row in DataSet
//THE NEXT LINE IS MY PROBLEM
string strSelect = "CtrlRef = " + sender;
DataRow[] foundRows;
foundRows = ds.Tables["datafield"].Select
(strSelect);
MessageBox.Show(foundRows.Length + " foundRows");
}

I have found that this works:
private void on_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
foreach (DataRow row in ds.Tables["datafield"].Rows)
{
if (row["CtrlRef"] == sender)
MessageBox.Show("found row by
reference!");
}
}

Please let me know if there is a way to use the
DataTable.Select method for this.

Thanks!

Lynn
 
L

Lynn Kear-Noe

Is there a way to use the hash value to reference the
control? I will want to find the record with a given tab
index, then use the reference to the control stored in
that record to give that control the focus.

Thanks,
Lynn


-----Original Message-----
Object references cannot be persisted between sessions. If you are only
interested in storing references for the session duration, make the column
int and use Object.GetHashValue() (or Control.GetHashValue())

Lynn Kear-Noe said:
I am creating controls at runtime according to
information stored in an SQL CE database. I fill a
dataset with the SQL CE records pertinent to the current
display page, then step through them and create the
controls. Each record describes one control. I want to
store a reference to the newly created control in another
field in that record. This seems to be working. But how
can I look up the record using the object reference?
DataTable.Select requires a select string, and I don't
know how to put the object reference in the select string
properly.

I create the new column like this (dt is type DataTable):
dt.Columns.Add("CtrlRef", System.Type.GetType
("System.Object"));

I store the object reference in the new column like this
(row is type DataRow, aTextBox is type TextBox):
row["CtrlRef"] = (object)aTextBox;

Then in a key event handler, I want to find the sender's
record in the DataTable

private void on_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
//find sender's row in DataSet
//THE NEXT LINE IS MY PROBLEM
string strSelect = "CtrlRef = " + sender;
DataRow[] foundRows;
foundRows = ds.Tables["datafield"].Select
(strSelect);
MessageBox.Show(foundRows.Length + " foundRows");
}

I have found that this works:
private void on_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
foreach (DataRow row in ds.Tables["datafield"].Rows)
{
if (row["CtrlRef"] == sender)
MessageBox.Show("found row by
reference!");
}
}

Please let me know if there is a way to use the
DataTable.Select method for this.

Thanks!

Lynn


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top