0

I have a DataGridView placed on a Panel, and the Panel's visible property is initially set to false. to set the Panel's visible property true, a button pressed.

This is the code that runs:

using(SqlConnection c = new SqlConnection(connStr))
{
    string query = "SELECT username, viewfldr, status FROM DirList ORDER BY viewfldr";
    DataTable tbl = new DataTable();
    using(SqlDataAdapter da = new SqlDataAdapter(query, c))
    {
        c.Open();
        da.Fill(tbl);
        c.Close();
    }
    dataGridView1.DataSource = tbl;
    dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Verdana", 10F, FontStyle.Bold);

    dataGridView1.Columns[0].HeaderText = "User Name";
    dataGridView1.Columns[1].HeaderText = "Folder";
    dataGridView1.Columns[2].HeaderText = "Status";
    dataGridView1.Columns[0].Width = 115;
    dataGridView1.Columns[1].Width = 342;
    dataGridView1.Columns[2].Width = 100;
    dataGridView1.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
panel2.Visible = false;
panel1.Visible = true;

I want to set global variable to values from the DataGridView each time the user selects a different row. In the SelectionChanged event I have:

int rw = dataGridView1.SelectedCells[0].RowIndex;
fld = dataGridView1.Rows[rw].Cells[1].Value.ToString();
usr = dataGridView1.Rows[rw].Cells[0].Value.ToString();

However each time I click the button to set the panel visible, I get the error:

Index was out of range. Must be non-negative and less than the size of the collection.

When I checked the table I noticed that it does have one record. I tried putting code in the Validating and Validated events to try and correct the error but nothing seems to work.

CDspace
  • 2,639
  • 18
  • 30
  • 36
  • Why do you assume a cell is selected? By the way, a DataAdapter can open and close a connection for you, so you don't need the `c.Open` and `c.Close` calls. – LarsTech Aug 10 '17 at 18:29
  • Your error message is pretty self explanatory. You're trying to access a row/column that doesn't exist. Which line in your code gives you the error? – Sach Aug 10 '17 at 18:35
  • yes the error message is self explanatory but I'm trying to access the data grid for the first time. I'm assuming these cells are selected because I have the SelectionMode property to FullRowSelect. The bottom line is I need to figure out a way to set an index before the error occurs. –  Aug 10 '17 at 18:41
  • Your assumption is off the mark. FullRowSelect just describes what happens when a cell is selected — the entire row is selected. I'm guessing you just want `int rw = 0;` and use an if-block to make sure you have records. – LarsTech Aug 10 '17 at 18:45
  • I guess that is what I'm trying to do. Before I executed the code I checked the data table row count before I set it to the datasource. The main thing is how do I set and keep the row index initially at 0 when I first try to access the datagridview by making the panel visible? –  Aug 10 '17 at 19:27
  • @MaxPlay Concerning Edit: You should have mentioned removing duplicate code. I almost rejected your edit. – jpaugh Aug 10 '17 at 19:35

0 Answers0