none
how to selectthe same row after re-populate? RRS feed

  • Question

  • I am using this code to populate data into dataGridView and I want to know how can select the same selected row when the user chhose to refresh by calling this procedure agaian?


    Code Block

    private void get_users()
    {
    data_table = new DataTable();
    // sql_connection = new SqlConnection("Data Source=.\\SQLEXPRESS;initial catalog=shefa;integrated security=true");
    sql_connection = new SqlConnection(public_var.sql_connection);
    sql_command = new SqlCommand("sp_get_all_users", sql_connection);
    sql_command.CommandType = CommandType.StoredProcedure;
    sql_adapter = new SqlDataAdapter(sql_command);
    sql_adapter.Fill(data_table);
    dataStaff.DataSource = data_table;

    dataStaff.Columns["file_no"].HeaderText = "file_no";
    dataStaff.Columns["file_no"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
    dataStaff.Columns["file_no"].Width = 160;
    dataStaff.Columns["file_no"].Visible = false;

    dataStaff.Columns["staff_category"].HeaderText = "staff_category";
    dataStaff.Columns["staff_category"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
    dataStaff.Columns["staff_category"].Width = 160;
    dataStaff.Columns["staff_category"].Visible = false;

    dataStaff.Columns["created_date"].HeaderText = "created_date";
    dataStaff.Columns["created_date"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
    dataStaff.Columns["created_date"].Width = 160;
    dataStaff.Columns["created_date"].Visible = false;

    dataStaff.Columns["id_number"].HeaderText = "id number";
    dataStaff.Columns["id_number"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
    dataStaff.Columns["id_number"].Width = 100;

    dataStaff.Columns["staff_active"].HeaderText = "active?";
    dataStaff.Columns["staff_active"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
    dataStaff.Columns["staff_active"].Width = 80;
    dataStaff.Columns["staff_active"].Visible = false;

    dataStaff.Columns["person_name"].HeaderText = "name";
    dataStaff.Columns["person_name"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
    dataStaff.Columns["person_name"].Width = 350;

    dataStaff.Focus();
    }

    Tuesday, January 22, 2008 1:51 PM

Answers

  • It is not clear from the code what dataStaff is, but most probably it is either ListView or DataGridView, none of which has SelectedIndex property. Instead, ListView has SelectedIndices and SelectedItems property while DataGridView has SelectedRows. Take the case of DataGridView, for example, use the code below to record which of the rows are selected:

     

    Code Block

    List<int> selectedIndices = new List<int>();

    foreach (DataGridViewRow row in dataStuff.SelectedRows)

    {

    selectedIndices.Add(row.Index);

    }

     

     

    After you updated the contents of the control, use the code below to restore selection:

     

    Code Block

    foreach (int index in selectedIndices)

    {

    if (index < dataStuff.Rows.Count)

    {

    dataStuff.Rows[index].Selected = true;

    }

    }

     

     

    Tuesday, January 22, 2008 4:12 PM

All replies

  • hello,
    Before setting the datasource, remember the SelectedIndex.
    After the bindinig, set the SelectedIndex back
    Tuesday, January 22, 2008 2:11 PM
  • It is not clear from the code what dataStaff is, but most probably it is either ListView or DataGridView, none of which has SelectedIndex property. Instead, ListView has SelectedIndices and SelectedItems property while DataGridView has SelectedRows. Take the case of DataGridView, for example, use the code below to record which of the rows are selected:

     

    Code Block

    List<int> selectedIndices = new List<int>();

    foreach (DataGridViewRow row in dataStuff.SelectedRows)

    {

    selectedIndices.Add(row.Index);

    }

     

     

    After you updated the contents of the control, use the code below to restore selection:

     

    Code Block

    foreach (int index in selectedIndices)

    {

    if (index < dataStuff.Rows.Count)

    {

    dataStuff.Rows[index].Selected = true;

    }

    }

     

     

    Tuesday, January 22, 2008 4:12 PM
  • Error    1    Using the generic type 'System.Collections.Generic.List<T>' requires '1' type arguments    C:\Users\Jassim\Documents\Visual Studio 2008\Projects\shefa\shefa\staff_admin_dashboard.cs    254    13    shefa



                List selectedIndices = new List();

                foreach (DataGridViewRow row in dataStaff.SelectedRows)
                {
                    selectedIndices.Add(row.Index);
                }





    Monday, January 28, 2008 7:46 PM
  • I edited the above post. Somehow the generic argument got lost. It should be List<int>

     

    Monday, January 28, 2008 9:52 PM