1

Sorry I'm struggling with this, I have tried all solutions listed here, none of them worked. I'm new to C# and I need your help with this, I got people scanning items to be entered in a datagridview control. with the code below I search DVG for a value and then add a new dvg row based on if the value exist or not.

public void InnerLoadData()
{
     if (textBox1.Text != string.Empty)
     {
         textBox1.DataBindings.Clear();
         textBox2.DataBindings.Clear();
         ScanID.DataBindings.Clear();
         CardNumber.DataBindings.Clear();
         Boolean result = true;

         string searchValue = ScanID.Text;
         dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
         foreach (DataGridViewRow row in dataGridView1.Rows)
         { 
             if ( row!=null && 
                row.Cells [3].Value != null && 
                row.Cells[3].Value.ToString() .Equals +(searchValue) && 
                row.Cells [4].Value.ToString()=="OUT" && 
                dataGridView1 !=null  )
             {
                 row.Selected = true;
                 row.SetValues(textBox1.Text, 
                     textBox2.Text,
                     CardNumber.Text,
                     ScanID.Text, 
                    "IN",
                     DateTime .Now );
                 row.DefaultCellStyle.BackColorColor.Green ;                           
                 result = false;
                 break ;
              }
         }

         if (result !=false )
         {                                  
             this.dataGridView1.Rows.Add(textBox1.Text, 
                textBox2.Text,
                CardNumber.Text,
                ScanID.Text, 
                "OUT",
                DateTime.Now); **// got error here**
             this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red ;                                       
         }
     }
 }

Here is my problem,

I save all the datagridview data to an sql table on FormClosing just in case, when I reload the data from sql and try adding new datagridview rows, I got the message above. Any idea how to fix it ? thank you.

private void reloadToolStripMenuItem_Click(object sender, EventArgs e)
{
    BindingSource bsdata1 = new BindingSource();
    dataGridView1 .AutoGenerateColumns = false;
    dataGridView1 .AutoGenerateColumns = false;
    //DataGridView1.DataSource = Nothing
    using (SqlCommand cmd = new SqlCommand("get_badge", _econnect))
    {  cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        ad.Fill(dt1);
        bsdata1.DataSource = dt1;
    dataGridView1 .DataSource  =  bsdata1 ;     
}
}}}}
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
Frank Belu
  • 27
  • 7

1 Answers1

3

You can't add rows to the DataGrid when it is already bound to dataTable.

You should add data into the DataTable itself.

Get DataTable from dataGrid.DataSource property(or better store it somewhere), use DataTable.NewRow method to create new row with the same schema, set its DataRow.ItemsArray property and add it to the same DataTable.

if (result !=false )
{     
     DataTable dt =  (DataTable)this.dataGridView1.DataSource;

     DataRow row = dt.NewRow();

     row.ItemsArray = new Object[]
     {
         textBox1.Text, 
         textBox2.Text,
         CardNumber.Text,
         ScanID.Text, 
         "OUT",
         DateTime.Now
     };

     dt.Rows.Add(row);                                      
 }

EDIT:

There are probably only two places where NullReferenceException may appear with such modifications - this.dataGridView1.DataSource is not set or CardNumber(ScanID) is null (if they are not static always on form controls of course).

Second case(if it is the case) is fixed by initializing those controls.
First case:

1. Either load dataTable when form initializes itself - Put reloadToolStripMenuItem_Click call in the end of form constructor.

2. Or just don't add anything when there are no DataSource:

if ((result != false) && 
    (this.dataGridView1.DataSource != null))
{  
     DataRow row = ...

3. If you really need to add items into empty non bound DataGridView, then you will have to use some dummy DataTable(or other DataSource) and merge it with loaded DataTable on reloadToolStripMenuItem_Click.

Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • I have tried that and it doesn't work, it gives me error" Reference not set to instance..." the normal routine is that user scan items and this items gets added to dvg. so when the application start with you code above, it gives me the reference error. Its only in case the application got closed that user can reload data from sql. – Frank Belu Sep 05 '14 at 19:02