Q: Scroll to a Row?

S

Soul

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.
 
E

Elefterios Melissaratos

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
 

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

Members online

Forum statistics

Threads
473,763
Messages
2,569,561
Members
45,035
Latest member
HoTaKeDai

Latest Threads

Top