Here is a crude example I built from that link I gave:
public partial class Form_DataGrid : Form
{
DataTable dt;
DataView dv;
CurrencyManager CM;
public Form_DataGrid()
{
InitializeComponent();
}
private void Form_DataGrid_Load(object sender, EventArgs e)
{
dt = BuildDynamicTableArrayExample();
dv = new DataView(dt);
dataGrid1.DataSource = dv;
dv.Sort = "FieldName1"; // sort field required for search (Find)...
// Initialize CurrencyManager to hold an instance of the form's CurrencyManager.
// Needed to manipulate the row pointer...
CM = (CurrencyManager)dataGrid1.BindingContext[dv];
}
private void button1_Click(object sender, EventArgs e)
{
int row = dv.Find(textBox1.Text); // find the text...
if (row >= 0)
{
CM.Position = row; // position the row pointer...
dataGrid1.Select(row); // if wanting to highlight the row too...
}
}
public static DataTable BuildDynamicTableArrayExample()
{
// Create new DataTable object...
DataTable dt = new DataTable();
dt.TableName = "MyDataTable"; // Optional unless serializing datatables
// Add some columns to the table...
dt.Columns.Add(new DataColumn("FieldName1", typeof(string)));
dt.Columns.Add(new DataColumn("FieldName2", typeof(int)));
dt.Columns.Add(new DataColumn("FieldName3", typeof(double)));
// Optional primary key field specifier...
dt.Columns["FieldName1"].Unique = true;
// generate some rows of data...
for (int r = 0; r < 10; r++)
{
// create a row object of table object's type defined above
DataRow dr = dt.NewRow();
// Set each column value in the row...
dr["FieldName1"] = "data " + r;
dr["FieldName2"] = r;
dr["FieldName3"] = (double)r;
// add the row to the table
dt.Rows.Add(dr);
}
return dt;
}
}
EDIT--NOTE: I do not
UnSelect(int rowID) any rows and although it will move the row pointer every time, it will also leave the previous row highlighted.