Need to put default values into a grid

  • Thread starter Thread starter Trey
  • Start date Start date
T

Trey

I am working on a Windows Application with a Grid. When someone starts a
new Row by entering selecting the empty row I need to put some default
information in the cells. The data is actually a date and time stamp for
the entry.

But I can't find an event to tell me when a new row is created so I can
enter the data into the cell.

Any suggestions?
 
Hi Trey,

Try something like:
private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)

{


if (dataGrid1.CurrentCell.RowNumber == sourceTable.Rows.Count)

{

BindingManagerBase bmb = dataGrid1.BindingContext[sourceTable];

DataRowView drv = (DataRowView)bmb.Current;

drv["Field"] = "DefaultValue";

}

}



In the if part I look if I am positioned in a new row (where rownumber
exceeds the rows in source table)

Then I simply find the BindingManagerBase responsible for grid and inserts
default value.

This method requires one slight modification though - it will fire whenever
user clicks in same new row - so you'll need to add a flag or something.
 
I tried this but when I did an update and accept changes the data never
appeared to get in the database. What could I be doing wrong?

Thanks,

Miha Markic said:
Hi Trey,

Try something like:
private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)

{


if (dataGrid1.CurrentCell.RowNumber == sourceTable.Rows.Count)

{

BindingManagerBase bmb = dataGrid1.BindingContext[sourceTable];

DataRowView drv = (DataRowView)bmb.Current;

drv["Field"] = "DefaultValue";

}

}



In the if part I look if I am positioned in a new row (where rownumber
exceeds the rows in source table)

Then I simply find the BindingManagerBase responsible for grid and inserts
default value.

This method requires one slight modification though - it will fire whenever
user clicks in same new row - so you'll need to add a flag or something.


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

Trey said:
I am working on a Windows Application with a Grid. When someone starts a
new Row by entering selecting the empty row I need to put some default
information in the cells. The data is actually a date and time stamp for
the entry.

But I can't find an event to tell me when a new row is created so I can
enter the data into the cell.

Any suggestions?
 
Hi Trey,

When are you updating to database?

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

Trey said:
I tried this but when I did an update and accept changes the data never
appeared to get in the database. What could I be doing wrong?

Thanks,

Miha Markic said:
Hi Trey,

Try something like:
private void dataGrid1_CurrentCellChanged(object sender,
System.EventArgs
e)

{


if (dataGrid1.CurrentCell.RowNumber == sourceTable.Rows.Count)

{

BindingManagerBase bmb = dataGrid1.BindingContext[sourceTable];

DataRowView drv = (DataRowView)bmb.Current;

drv["Field"] = "DefaultValue";

}

}



In the if part I look if I am positioned in a new row (where rownumber
exceeds the rows in source table)

Then I simply find the BindingManagerBase responsible for grid and inserts
default value.

This method requires one slight modification though - it will fire whenever
user clicks in same new row - so you'll need to add a flag or something.


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

Trey said:
I am working on a Windows Application with a Grid. When someone
starts
 
In the CurrentCell_Changed event.

Miha Markic said:
Hi Trey,

When are you updating to database?

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

Trey said:
I tried this but when I did an update and accept changes the data never
appeared to get in the database. What could I be doing wrong?

Thanks,

Miha Markic said:
Hi Trey,

Try something like:
private void dataGrid1_CurrentCellChanged(object sender,
System.EventArgs
e)

{


if (dataGrid1.CurrentCell.RowNumber == sourceTable.Rows.Count)

{

BindingManagerBase bmb = dataGrid1.BindingContext[sourceTable];

DataRowView drv = (DataRowView)bmb.Current;

drv["Field"] = "DefaultValue";

}

}



In the if part I look if I am positioned in a new row (where rownumber
exceeds the rows in source table)

Then I simply find the BindingManagerBase responsible for grid and inserts
default value.

This method requires one slight modification though - it will fire whenever
user clicks in same new row - so you'll need to add a flag or something.


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

I am working on a Windows Application with a Grid. When someone
starts
a
new Row by entering selecting the empty row I need to put some default
information in the cells. The data is actually a date and time
stamp
for
the entry.

But I can't find an event to tell me when a new row is created so I can
enter the data into the cell.

Any suggestions?
 
Hi Trey,

I thought so.
You have to know that databinding mechanism uses a buffered row when doing
edit.
Thus the row is not yet stored in datatable until you call (or implicitly)
EndCurrentEdit method of BindingManagerBase.

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

Trey said:
In the CurrentCell_Changed event.

Miha Markic said:
Hi Trey,

When are you updating to database?

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

Trey said:
I tried this but when I did an update and accept changes the data never
appeared to get in the database. What could I be doing wrong?

Thanks,

"Miha Markic" <miha at rthand com> wrote in message
Hi Trey,

Try something like:
private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs
e)

{


if (dataGrid1.CurrentCell.RowNumber == sourceTable.Rows.Count)

{

BindingManagerBase bmb = dataGrid1.BindingContext[sourceTable];

DataRowView drv = (DataRowView)bmb.Current;

drv["Field"] = "DefaultValue";

}

}



In the if part I look if I am positioned in a new row (where rownumber
exceeds the rows in source table)

Then I simply find the BindingManagerBase responsible for grid and inserts
default value.

This method requires one slight modification though - it will fire
whenever
user clicks in same new row - so you'll need to add a flag or something.


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

I am working on a Windows Application with a Grid. When someone starts
a
new Row by entering selecting the empty row I need to put some default
information in the cells. The data is actually a date and time stamp
for
the entry.

But I can't find an event to tell me when a new row is created so
I
can
enter the data into the cell.

Any suggestions?
 
Yea, I just found out that it put the data in the first row. Do you know
how to put data into this buffered row?

Miha Markic said:
Hi Trey,

I thought so.
You have to know that databinding mechanism uses a buffered row when doing
edit.
Thus the row is not yet stored in datatable until you call (or implicitly)
EndCurrentEdit method of BindingManagerBase.

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

Trey said:
In the CurrentCell_Changed event.

Miha Markic said:
Hi Trey,

When are you updating to database?

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

I tried this but when I did an update and accept changes the data never
appeared to get in the database. What could I be doing wrong?

Thanks,

"Miha Markic" <miha at rthand com> wrote in message
Hi Trey,

Try something like:
private void dataGrid1_CurrentCellChanged(object sender,
System.EventArgs
e)

{


if (dataGrid1.CurrentCell.RowNumber == sourceTable.Rows.Count)

{

BindingManagerBase bmb = dataGrid1.BindingContext[sourceTable];

DataRowView drv = (DataRowView)bmb.Current;

drv["Field"] = "DefaultValue";

}

}



In the if part I look if I am positioned in a new row (where rownumber
exceeds the rows in source table)

Then I simply find the BindingManagerBase responsible for grid and
inserts
default value.

This method requires one slight modification though - it will fire
whenever
user clicks in same new row - so you'll need to add a flag or something.


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

I am working on a Windows Application with a Grid. When someone
starts
a
new Row by entering selecting the empty row I need to put some default
information in the cells. The data is actually a date and time stamp
for
the entry.

But I can't find an event to tell me when a new row is created
so
 
Trey said:
Yea, I just found out that it put the data in the first row. Do you know
how to put data into this buffered row?

Look at my anwser few answers ago :)
 
I must be really dense on this.

Here is my code. But when I look at the DataRowView (drv) it is pointing to
the first row in the database not the row that the grid is editing. I don't
see that EndCurrentEdit will make any difference in which row I am editing.
Remember it is the last row so it is a new row and has not been added to the
database or maybe I am doing something completly wrong.

private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)

{

if (dataGrid1.CurrentCell.RowNumber == myTable.Rows.Count)

{

BindingManagerBase bmb =dataGrid1.BindingContext[ds.Tables["CallLog"]];

DataRowView drv = (DataRowView)bmb.Current;

drv["Company"] = "test company";

}

}
 
Back
Top