Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > how to fill the fields of a window form while selecting a row in datagirdview control?
 

how to fill the fields of a window form while selecting a row in datagirdview control?

i have a problem in the following piece of code. i made a window form window form with some controls like textboxes comboBoxes and datetimepicker and buttons. i made a edit button whose functionality is that when i select a row in datagridview and press edit botton the allthe fields are filled byrecord in that row but i got an exception whiled doing this. The exception is NullReferenceException on the line which i have bold in the following piece of code. Kindly anyone can help me out from this problem why this exception is occuring and correct the code if there is mistake?


private

void Edit_Click(object sender, EventArgs e)
{

DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows;
foreach (DataGridViewRow rw in rows)
{

id =

int.Parse(rw.Cells[0].Value.ToString());
comboBox3.Text = rw.Cells[1].Value.ToString();
comboBox2.Text = rw.Cells[2].Value.ToString();
comboBox1.Text = rw.Cells[3].Value.ToString();
textBox1.Text = rw.Cells[4].Value.ToString();
richTextBox1.Text = rw.Cells[5].Value.ToString();
dateTimePicker1.Text = rw.Cells[6].Value.ToString();
}
}

  • Moved bynobugzMVP, ModeratorTuesday, July 28, 2009 1:10 PM (From:64-Bit .NET Framework Development.)
  •  
Sheeraz Raza  Tuesday, July 28, 2009 1:03 PM
may be the column contain null values
before bind the value to the control.just ensure that dg columnvalue isnotnull or empty
like
if(rw.Cells[1].Value!=null)
{
comboBox3.Text = rw.Cells[1].Value.ToString();

}
else
{
do something..
}

Cheers, C.Gnanadurai
Gnanadurai  Tuesday, July 28, 2009 1:46 PM
may be the column contain null values
before bind the value to the control.just ensure that dg columnvalue isnotnull or empty
like
if(rw.Cells[1].Value!=null)
{
comboBox3.Text = rw.Cells[1].Value.ToString();

}
else
{
do something..
}

Cheers, C.Gnanadurai
Gnanadurai  Tuesday, July 28, 2009 1:46 PM

You can use google to search for other answers

Custom Search

More Threads

• Form data binding control not updating on entry of text
• DataGridView Row Header Mouse Double Click Event, Visual Studio 2005
• DataView problem?
• Locate a form on screen?
• Adding menu item to the default popup menu in datagrid?
• DataGrid query
• Binding to RichTextBox
• Progress bar and Databinding
• Binding Combos to dataset support tables
• handling events in my dataGridView