Question

I have a dataGridView and its getting all of its data from the database. What I did is I create a method to show all data but the thing is, after calling the method, the scroll returns to the top which I dont want to happen. I created this method for the purpose of showing the newly added data and for updating the color.

    private void showAllData()
    {
        try
        {
            using (OleDbConnection conn = new OleDbConnection(GlobalVar.connectionString))
            using (OleDbDataAdapter sda = new OleDbDataAdapter("SELECT * FROM tblTest", conn))
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);

                dataGridView1.DataSource = dt;

                for (int x = 0; x < dataGridView1.RowCount; x++)
                {
                    if (dataGridView1.Rows[x].Cells["Color"].Value == "Green")
                        dataGridView1.Rows[x].DefaultCellStyle.BackColor = Color.Green;
                    else
                        dataGridView1.Rows[x].DefaultCellStyle.BackColor = Color.Red;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            using (OleDbConnection conn = new OleDbConnection(GlobalVar.connectionString))
            using (OleDbCommand cmd = new OleDbCommand("INSERT INTO tblTest (Color) VALUES ('Red')", conn))
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        //Update dataGridView1 BackColor
        showAllData();
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            using (OleDbConnection conn = new OleDbConnection(GlobalVar.connectionString))
            using (OleDbCommand cmd = new OleDbCommand("UPDATE tblTest SET Color = 'Green' WHERE id = " + dataGridView1.SelectedRows[0].Cells[0].Value, conn))
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        //Update dataGridView1 BackColor
        showAllData();
    }
Was it helpful?

Solution

Use the DataGridView.FirstDisplayedScrollingRowIndex property. It sets the index of the row that is the first row displayed on the DataGridView.

In your case since you are changing the DataSource, new rows might have been added. You can't use the already selected row index. You have to identify the row which was selected earlier (using any unique identifier) and get its index and set the above property to this index number.

Instead of resetting the DataSource, you could also merge the queried table with your existing table using DataTable.Merge and refresh the grid using DataGridView.Refresh.

Or you can fill the original DataTable again.

DataTable origTable = dataGridView1.DataSource as DataTable;
sda.Fill(origtable);
dataGridView1.Refresh();

OTHER TIPS

You just need to set the CurrentCell after you do your refresh:

var ptCurrentCell = this.DataGridView1.CurrentCellAddress();

--do your refresh of data

Refresh();

--now reset the current cell

this.DataGridView1.CurrentCell = this.DataGridView1.Rows(ptCurrentCell.Y).Cells(ptCurrentCell.X);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top