RE: Which DataGridViewRow?

Giganews Newsgroups
Subject: RE: Which DataGridViewRow?
Posted by:  v-zh…
Date: Thu, 18 Dec 2008

Hi Ed,

Thank you for using Microsoft Managed Newsgroup Service, it's my pleasure
to work with you on this issue.

To manage the row position between the source and DataGridView, I would
recommend you bind the DataGridView to a DataTable instead, then you can
manage the position via the underlying DataView object.

I write the following sample code for your information.

void Form1_Load(object sender, EventArgs e)
            DataTable dt = new DataTable("dt");
            dt.Columns.Add("c1", typeof(int));
            for (int j = 0; j < 10; j++)
                dt.Rows.Add(j, "aaa" + j.ToString());


            this.dataGridView1.DataSource = ds.Tables[0];

        DataSet ds = new DataSet();

        private void button1_Click(object sender, EventArgs e)
            DataTable dt = this.dataGridView1.DataSource as DataTable;
            DataView dv = dt.DefaultView;

          // Get the coresponding row in the source to the first row in
the DataGridView
            DataRow r = dv[0].Row;
            int IndexInSource = dt.Rows.IndexOf(r);
                "The first row in the DGV is the {0} row in the data
", IndexInSource));

        private void button2_Click(object sender, EventArgs e)
            DataTable dt = this.dataGridView1.DataSource as DataTable;
            DataView dv = dt.DefaultView;
            if (!string.IsNullOrEmpty(dv.Sort))
                //get the column name that is currently sorted;
                string c = dv.Sort.Replace("[", "");
                c = c.Replace("]", "");
                int index = c.IndexOf(' ');
                if (index > 0)
                    c = c.Substring(0, index);
                int indexInDGV = dv.Find(dt.Rows[0][c]);
                    "The first row in the data source is the {0} row in the
", indexInDGV));

Ed, please try my suggestion and let me know whether it make sense to you,
if you have any question or concern, please do not hesitate to let me know.

Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:

Get notification to my posts through email? Please refer to

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
This posting is provided "AS IS" with no warranties, and confers no rights.


In response to

Which DataGridViewRow? posted by Ed White on Wed, 17 Dec 2008