Click here to Skip to main content
16,020,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi every body i have agridview where i need to fill all row coloumn from one of the column when i open the form and before moving to the column where i will put data and the other column binnding from it i get the following error NullReferenceException (Object reference not set to an instance of an object)
my code is
C#
private void Form_Load(object sender, EventArgs e)
      {
          this.dataGridView1.CellValidated += new DataGridViewCellEventHandler(dataGridView1_CellValidated);

          dataGridView1.RowPostPaint += dataGridView1_RowPostPaint;


      }

   private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
      {
          int count = dataGridView1.Columns.Count;


          if (e.RowIndex >= 0)
          {
              if (!string.IsNullOrEmpty(dataGridView1.Rows[e.RowIndex].Cells["txtproname"].Value.ToString()))
              {
                  _id1 = dataGridView1.Rows[e.RowIndex].Cells["txtproname"].Value.ToString();
                  PopulateColumns();
                  dataGridView1.Rows[e.RowIndex].Cells[3].Value = _barcode;
                  dataGridView1.Rows[e.RowIndex].Cells[4].Value = _price;
                  dataGridView1.Rows[e.RowIndex].Cells[5].Value = _unit;
              }
          }

      }


the error gets here
if (!string.IsNullOrEmpty(dataGridView1.Rows[e.RowIndex].Cells["txtproname"].Value.ToString()))

how can i be solved?? need help
Posted

1 solution

The idea of using String.IsNullOrEmpty(..) is alright - but before this method does its checking, you access the potentially "null-containing" property .Value with .ToString().

If the cell can only contain a String or null, then replace it with a simple cast to String and it'll work (or, at least it won't cause the exception any more):
C#
if (!string.IsNullOrEmpty((string)dataGridView1.Rows[e.RowIndex].Cells["txtproname"].Value))


If the cell can contain contain something else than a string or null, then it's just a tiny bit more complex:
C#
object cellValue = dataGridView1.Rows[e.RowIndex].Cells["txtproname"].Value;
if (cellValue != null && !string.IsEmpty(cellValue.ToString()))
 
Share this answer
 
v5
Comments
Maciej Los 16-May-15 6:27am    
Sounds perfectly ;)
Sascha Lefèvre 16-May-15 6:39am    
Actually it wasn't quite, *cough*, but now it should be ;-)
ost3z 16-May-15 8:48am    
thank you too much it worked fine but there is aproblem happened with my combox the value get clear on living the cell
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{


string titleText = dataGridView1.Columns[2].HeaderText;
if (titleText.Equals("proname"))
{
ComboBox cbo = e.Control as ComboBox;
if (cbo != null)
{
cbo.DropDownStyle = ComboBoxStyle.DropDown;
cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
cbo.AutoCompleteCustomSource = AutoCompleteLoad();
cbo.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}


}

thank again for helping
Sascha Lefèvre 16-May-15 9:09am    
You're welcome! Glad I could help :)

But unfortunately I can't help you with this follow-up problem. Please post it as a new question so someone else can help you there :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900