I have three buttons which loads different tables into datagridview. Each table has different amount of textboxes. I know, that one of the ways to connect textboxes and dgv is
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
if (dataGridView1.Columns.Count == 5)
{
textBox3.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
textBox4.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
//textBox5.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
//richTextBox1.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
//richTextBox2.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
}
if (dataGridView1.Columns.Count == 6)
{
textBox3.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
textBox4.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
//textBox5.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
//textBox6.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
//richTextBox1.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
//richTextBox2.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
}
else
{
textBox3.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
textBox4.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
}
}
}
But its kinda long code and i want to try to make it clearer. I want to use array of textboxes (if its even possible?), I tried this:
int i = 1;
TextBox[] textboxes = new TextBox[i];
textboxes[0] = textBox1; textboxes[1] = textBox2;
textboxes[2] = textBox2; textboxes[3] = textBox3;
for (int j = 1; i < textboxes.Length; i++)
{
//i dont know how to jump to the next cell of current row in the dtg
}
Are there any other options besides textbox arrays?
