Find and Select a row in a DataGrid

  • Thread starter Thread starter Ibai Peña
  • Start date Start date
I

Ibai Peña

Hi,

How can I find a value in a DataGrid (in a specific column), and select its
row?

I have found the DataGrid.Select(int irowSel) method, but I dont know the
index of the row to be selected.
Must I iterate throw the DataGrid and look for the value in each row? is
there any other way?

Thanks in advance,
 
If your grid is bound to dataview, you can use DataView.Find
 
Ibai Peña said:
Hi,

How can I find a value in a DataGrid (in a specific column), and select its
row?

I have found the DataGrid.Select(int irowSel) method, but I dont know the
index of the row to be selected.
Must I iterate throw the DataGrid and look for the value in each row? is
there any other way?

Thanks in advance,

If you can't use .IndexOf()

These work:

private void ComboByValue(System.Windows.Forms.ComboBox combo,
string defaultValue)
{
try
{
for (int i = 0; i < combo.Items.Count; i++)
{
combo.Visible = false;
combo.SelectedIndex = i;
if (defaultValue == combo.SelectedValue.ToString()) break;
}
combo.Visible = true;
}
catch{combo.SelectedIndex = 0;}
}

private void ComboByText(System.Windows.Forms.ComboBox combo, string
defaultValue)
{
try
{
if (defaultValue == string.Empty)
{
combo.SelectedIndex = -1;
return;
}
for (int i = 0; i < combo.Items.Count; i++)
{
combo.Visible = false;
combo.SelectedIndex = i;
if (defaultValue == combo.Text) break;
}
combo.Visible = true;
}
catch{combo.SelectedIndex = 0;}
}
 
Ibai Peña said:
Hi,

How can I find a value in a DataGrid (in a specific column), and select its
row?

I have found the DataGrid.Select(int irowSel) method, but I dont know the
index of the row to be selected.
Must I iterate throw the DataGrid and look for the value in each row? is
there any other way?

Thanks in advance,

Sorry - wrong control in that last reply, but the concept applies.
 

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

Back
Top