views:

409

answers:

4

this is my code:

        private void getData(string selectCommand)
    {
        string connectionString = @"Server=localhost;User=SYSDBA;Password=masterkey;Database=C:\data\test.fdb";

        dataAdapter = new FbDataAdapter(selectCommand, connectionString);
        DataTable data = new DataTable();
        dataAdapter.Fill(data);
        bindingSource.DataSource = data;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        getData(dataAdapter.SelectCommand.CommandText);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.DataSource = bindingSource;
        getData("SELECT * FROM cities");
    }

after reload data on button1 click, cell selection jumps on first column and scrollbars is reset. How to save position of DataGridView?

+1  A: 

You could save selected row before launching getData, using DataGridView.CurrentRow, and select that row after the Grid has been loaded.

In this question I answered how to select an specific row in a DataGridView.


Edit: I supposed that you are using WinForms

Edit2: And what about scrollbars?

You can save the first visible row index, too with this statement

DataGridView.FirstDisplayedCell.RowIndex
Javier Morillo
A: 

Currently, you are loading the data everytime the page is loaded. I would suggest using the Page.IsPostback property to check and see if it is a postback or not.

if(!Page.IsPostback)
{
    dataGridView1.DataSource = bindingSource;  
    getData("SELECT * FROM cities");
}

This will reduce the amount of load on your DB and will quit causing issues with your selections.

Lucas
This Page.IsPostback property is valid only for ASP.NET applications. We're here talking about WinForms.
Will Marcouiller
A: 

This does so because you're resetting or reaffecting the DataSource property of your DataGridView control.

In order to perform what you wish, you have to save the CurrentItem index into a local variable before resetting the DataSource property of your DataGridView. However, doing so implies that you know you're going to have the same or more amount of data, otherwise you will come out with an IndexOutOfRangeException trying to move to a greater index than the amount of data actually contained in your DataGridView.

So, if you wish to save the index of your row or cell, you will need to make it through the DataGridView control properties as your BindingSource wil not deliver such feature.

In a DataGridView, the selected row and the current row (indicated by an arrow in the row header) may not be the same row. In addition, we could select multiple rows in a DataGridView but the current row can only be one row. When the SelectionMode property of the DataGridView is set to FullRowSelect, the current row will be always selected. If you'd like to change the current row in a DataGridView control, you may set the CurrentCell property

dataGridView1.CurrentCell = dataGridView1.Rows[1].Cells[0]; 

If you'd like to just change the selected row, you may set the Selected property of the row you want to true.

dataGridView1.CurrentRow.Selected = false; 
dataGridView1.Rows[1].Selected = true; 
Will Marcouiller
And what with scroll bars? If the user simply scroll datagrid without selecting a cell.
bobik
Then neither the DataGridView, nor the BindingSource can identify where the user is scrolling. When you load data and set it to the DataSource property of your DataGridView, you tell the WinForm to refresh with new data, then everything is reinitialized. I know of no other way then with a selected cell or row. Windows itself does not behave differently.
Will Marcouiller
@bobk, I answer this in my post, in the last edit.
Javier Morillo
A: 

At another forum, I found a solution without any manipulation:

    private void getData(string selectCommand)
    {
        string connectionString = @"Server=localhost;User=SYSDBA;Password=masterkey;Database=C:\data\test.fdb";

        dataAdapter = new FbDataAdapter(selectCommand, connectionString);
        data = new DataTable();
        dataAdapter.Fill(data);
        bindingSource.DataSource = data;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
        dataAdapter.Fill(data);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.DataSource = bindingSource;
        getData("SELECT * FROM cities");
    }
bobik