Object Reference Not Set error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • inpuarg

    #1

    Object Reference Not Set error

    I am trying to insert a row to a specific location to a datagridview at bound
    mode. For implementing this feature i am adding a new column to grid 's
    dataview's table. Setting sort index. Then sort by using that new column. Then
    remove that new column. It seems working fine.


    I run following code for the first time without error. But if i run 2'nd time i
    got Object Reference Not Set to an instance of an object error at line :
    dr.Cells["SO"].Value = i + 1;

    What is the reason ?


    public bool InsertRow()
    {
    if (base.CurrentRo w!=null)
    {
    int curRowIndex = base.CurrentCel l.RowIndex ;
    int curColIndex = base.CurrentCel l.ColumnIndex ;

    DataColumn dcSort = new DataColumn("SO" ,
    System.Type.Get Type("System.In t32"));
    DataView dv =
    ((CurrencyManag er)base.Binding Context[base.DataSource , base.DataMember]).List as
    DataView;
    dv.Table.Column s.Add(dcSort);


    BindingManagerB ase bm =base.BindingCo ntext[base.DataSource ,
    base.DataMember];
    DataRowView dvr = (DataRowView)bm .Current;
    DataView dvn = dvr.DataView;
    int i = -1;
    foreach (DataGridViewRo w dr in base.Rows )
    {
    i++;
    if (i < curRowIndex + 1)
    {
    dr.Cells["SO"].Value = i + 1;
    }
    else
    {
    dr.Cells["SO"].Value = i + 2;
    }
    }

    DataRow row = dv.Table.NewRow ();
    row["ID"] = System.Guid.New Guid().ToString ().ToUpper();
    row["SO"] = curRowIndex + 2;
    dv.Table.Rows.I nsertAt(row, curRowIndex);
    dv.Table.Accept Changes();
    ListSortDirecti on direction = ListSortDirecti on.Ascending;
    DataGridViewCol umn sortColumn = base.Columns["SO"];
    base.Sort(sortC olumn, direction);
    base.Columns.Re move(sortColumn );
    dv.Table.Column s.Remove(dcSort );

    base.CurrentCel l = base[curColIndex, curRowIndex + 1];
    base.Refresh();
    }
    return true;
    }

Working...