471,306 Members | 1,290 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

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.CurrentRow!=null)
{
int curRowIndex = base.CurrentCell.RowIndex ;
int curColIndex = base.CurrentCell.ColumnIndex ;

DataColumn dcSort = new DataColumn("SO",
System.Type.GetType("System.Int32"));
DataView dv =
((CurrencyManager)base.BindingContext[base.DataSource, base.DataMember]).List as
DataView;
dv.Table.Columns.Add(dcSort);
BindingManagerBase bm =base.BindingContext[base.DataSource,
base.DataMember];
DataRowView dvr = (DataRowView)bm.Current;
DataView dvn = dvr.DataView;
int i = -1;
foreach (DataGridViewRow 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.NewGuid().ToString().ToUpper();
row["SO"] = curRowIndex + 2;
dv.Table.Rows.InsertAt(row, curRowIndex);
dv.Table.AcceptChanges();
ListSortDirection direction = ListSortDirection.Ascending;
DataGridViewColumn sortColumn = base.Columns["SO"];
base.Sort(sortColumn, direction);
base.Columns.Remove(sortColumn);
dv.Table.Columns.Remove(dcSort);

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

Jan 13 '06 #1
0 2030

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Pkpatel | last post: by
7 posts views Thread by Brett | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.