-1

I am using the code below to retrieve values from the selected row it works... but has a glitch.... It will only retrieve the strings if you click on the actual text in any cell of that row.... if you click anywhere of white area in a cell of that row it will not execute .. Any ideas as to how to fix this glitch??

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            listBox1.ClearSelected();
            OnlineNamebox.Text = "";
            OnlinePasswordbox.Text = "";
            OnlineEmailbox.Text = "";
            OnlineShortcodebox.Text = "";
            ListCombobox.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            OnlineNamebox.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            OnlineEmailbox.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            OnlinePasswordbox.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            OnlineShortcodebox.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
                
            

        }
  • You shouldn't need any code at all. Just bind your grid to a list of items, be it a `DataTable` or something else, and bind your other controls to the same list. Making a selection in the grid will then automatically populate the controls with the data for that list item. – John Aug 22 '22 at 03:52

1 Answers1

0

I found a work around for obtaining the cell values based off of dash's answer on a kind of similar question using the cellmouseclick instead of cellclick

with the code below it dont matter where you click at in the row it executes the strings even if not directly clicking on the text in a cell

private void DatagridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)
        {
            ListCombobox.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            OnlineNamebox.Text = dataGridView1.CurrentRow.Cells[1].FormattedValue.ToString();
            OnlineEmailbox.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            OnlinePasswordbox.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            OnlineShortcodebox.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();

            
        }

and I added the code below to the load event

dataGridView1.CellMouseClick += DatagridView1_CellMouseClick;

hope it helps anyone who runs into this particular problem or that is looking for the basics