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.

Recommended Answers

All 2 Replies

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());
   }
}

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
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.