Add row to datagrid winform

N

Nick

Hi,

I have a winform that displays header information related
to a task the user selected to work on. On the form there
is a datagrid that has detail rows for the task (e.g.
date, note, duration columns). The datagrid is bound to a
dataset. The datarow has a taskid column that is not
shown in the datagrid.

Updating data in the grid works fine. But, when I start a
new row in the datagrid, populate the three columns
(date, note, duration) and then try to go to another
control, which causes the row to lose focus and try to
save the new datarow, I get a 'can not insert a null
value in the taskid column' which makes total sense.

The user should not need to enter (or see) the taskid in
datagrid row as the taskid is already on the form.

I could not find an event in any of the related objects
that allows me to add the taskid to the taskid column
just before the datarow insert to avoid the error.

I finally found away around the null error by setting the
columns default value to the task id which does the
trick.

I was wondering if anyone knew other techniques to handle
the above. I would have expected and event somewhere in
the datarow, datatable, dataset, databinding manager,
etc. objects that allow the hook I need to perform the
above.

Thanks Nick
 
M

Miha Markic

Hi Nick,

One solution would be to implement BindingManagerBase.PositionChanged event.
Example for NWind Categories table:
// bind to event
BindingManagerBase bmb = BindingContext[dataSet11.Categories];

bmb.PositionChanged += new EventHandler(bmb_PositionChanged);

// event

private void bmb_PositionChanged(object sender, EventArgs e)

{

BindingManagerBase bmb = BindingContext[dataSet11.Categories];

if (bmb.Position == dataSet11.Categories.Rows.Count) // check if row is
outside DataTable.Rows.Count -> new row if it is

{

DataRowView row = bmb.Current as DataRowView;

if (row.Row.IsNull("CategoryName"))

row["CategoryName"] = "Tubo";

}

}
 
N

Nick

Hi Miha,

I tried your solution and that did the trick!!

Thanks a lot.

I did try the Row_Changing event that fired before the
validation error, but I got another error "that can not
change proposed value in event".

private void Row_Changing( object sender,
DataRowChangeEventArgs e )
{

if (e.Action == DataRowAction.Add)
{
// get can not change proposed value in event error
e.Row["TaskID"] = m_TaskRow["TaskID"];
}

}

Thanks Again!! Nick


-----Original Message-----
Hi Nick,

One solution would be to implement
BindingManagerBase.PositionChanged event.
Example for NWind Categories table:
// bind to event
BindingManagerBase bmb = BindingContext [dataSet11.Categories];

bmb.PositionChanged += new EventHandler (bmb_PositionChanged);

// event

private void bmb_PositionChanged(object sender, EventArgs e)

{

BindingManagerBase bmb = BindingContext [dataSet11.Categories];

if (bmb.Position ==
dataSet11.Categories.Rows.Count) // check if row is
outside DataTable.Rows.Count -> new row if it is

{

DataRowView row = bmb.Current as DataRowView;

if (row.Row.IsNull("CategoryName"))

row["CategoryName"] = "Tubo";

}

}


--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Hi,

I have a winform that displays header information related
to a task the user selected to work on. On the form there
is a datagrid that has detail rows for the task (e.g.
date, note, duration columns). The datagrid is bound to a
dataset. The datarow has a taskid column that is not
shown in the datagrid.

Updating data in the grid works fine. But, when I start a
new row in the datagrid, populate the three columns
(date, note, duration) and then try to go to another
control, which causes the row to lose focus and try to
save the new datarow, I get a 'can not insert a null
value in the taskid column' which makes total sense.

The user should not need to enter (or see) the taskid in
datagrid row as the taskid is already on the form.

I could not find an event in any of the related objects
that allows me to add the taskid to the taskid column
just before the datarow insert to avoid the error.

I finally found away around the null error by setting the
columns default value to the task id which does the
trick.

I was wondering if anyone knew other techniques to handle
the above. I would have expected and event somewhere in
the datarow, datatable, dataset, databinding manager,
etc. objects that allow the hook I need to perform the
above.

Thanks Nick


.
 
K

koo9

Miha

you are better than the M$ MVPs, I post my question on the ms
newsgroup, nobody can answer it. now I found my solution here. you
are a life saver! excellent job.


Kevin
 
K

koo9

one mor question though, if I set up datarelation between parent and
child tables, shouldn't the datarelation take care of the parent
foreign key column in the new child row?

thx


Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top