0

When I select few records from datagridview and insert into access database. First selected record will last insert into database and last selected records will insert first.

I got give a number to each record. For example, I got 3 items. When I select this 3 items and insert to database, the last item will get number 1 and first item will get number 3.

How can I let first item get number 1, second item get number 2 and so on.

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by Mitja Bonca
0

How are you selecting the records from the DataGridView?

If you select them in a loop (or other primitive manner), you can dictate which one is first.

private void Form1_Load(object sender, EventArgs e)
{
   dataGridView1.DataSource =
      (from s in new List<string> { "alpha", "bravo", "charlie" }
       select new { name = s }).ToList();
}

private void button1_Click(object sender, EventArgs e)
{
   for (int i = 0; i < dataGridView1.Rows.Count; i++ )
   {
      MessageBox.Show(dataGridView1.Rows[i].Cells[0].Value.ToString());
   }
}
0

use for loop, but instead of incrementing +1, do the reverse operation, so each step -1:

            for (int i = dgv.Rows.Count; i > 0; i--)
            {
                DataGridViewRow row = dgv.Rows[i] as DataGridViewRow;
                //use row for your insertion
            }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.