Soul said:
Hi,
I have DataGrid on a C# Windows Application. Can I know how can I scroll to
a particular row in the DataGrid?
Thank you.
Hi Soul
FYI this is the group for ASP.NET datagrid and not for Windows Forms.
However here is my answer to your question:
You have to use the concept of the CurrencyManager in windows forms.
Then you can scroll to a specific row by aasigning the appropriate value
to the Position property of the CurrencyManager object.
Lets say that you bind DataTable dt to a Datagrid dataGrd. Then
you have :
DataGrd.DataSource = dt;
cm = (CurrencyManager)this.BindingContext[dt];
cm.Position = 2; // scrolls to the second row of the datagrid
I include the full code below:
On the Load event of your form populate the datagrid see below
private void DatagridBinding_Load(object sender, System.EventArgs e)
{
// Create a Datatable to bind it to the Datagrid
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("FirstName",System.Type.GetType("System.String"));
dt.Columns.Add(dc);
dc = new DataColumn("LastName",System.Type.GetType("System.String"));
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr["FirstName"] = "Donald";
dr["LastName"] = "Knuth";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["FirstName"] = "Richard";
dr["LastName"] = "Karp";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["FirstName"] = "Jim";
dr["LastName"] = "Gray";
dt.Rows.Add(dr);
DataGrd.DataSource = dt;
cm = (CurrencyManager)this.BindingContext[dt];
cm.Position = 0;
}
and then you can scroll to row 1 by cm.Position = 1 etc.
Regards
Lefteris