C# .NET - Default Values for New Rows in the Windows Forms DataGrid

Asked By C E on 31-Jan-10 06:30 AM
Hi, 

I'm working on a windows forms application written with the framework 1.1 and i can't figure out how i can do this : 

http://msdn.microsoft.com/en-us/library/b22t666e.aspx

Unfortunately with the framework 1.1 there is not such DefaultValuesNeeded event.
In other words i have an editable Datagrid (not DatagridView), the user inputs the the values directly inside the datagrid but i need to set a default dynamic value for a certain key field for each new row is added in real time to the datasource.

I tried the event OnRowChanging and OnRowChanged of the DataTable but it doesn't seem to work, the event is not raised when i need it to be raised (at least in the way i used it).
Anyone have any idea on how to manage this quite common situation with the version of the framework i am using?

Thanks in advance for the help.
C.
Sakshi a replied to C E on 31-Jan-10 07:29 AM

suggested options:

1. You may need to extend the DataGrid if you want custom options like this.

or

2. Better to upgrade to latest .net framework


Thanks and Regards,
http://www.CodeCollege.NET
http://www.InterviewsGuru.info

Use the DataColumn's Default Value property.

[)ia6l0 iii replied to C E on 01-Feb-10 12:17 AM
One would use the DataColumn's Default Value property to provide default values to the new rows like in the DefaultValuesNeeded event of the DataGridView. 

You would get this from the underlying datasource of the DataGrid - The DataTable.

For e.g. the DefaultValue of the date column would be assigned like this:
myDataSet.Tables["boundDataTable"].Columns["boundColumn"].DefaultValue = DateTime.Now; 

Hope this information helps.
Huggy Bear replied to C E on 01-Feb-10 01:42 AM
In addition to diablo's solution, there is a way to add default values only to your new rows. You can make use of the RowChanged event and check the e.Action to be DataRowAction.Add. Below is the sample code, which would give you a good idea.

Private Sub dataTable_RowChanged(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles dataTable.RowChanged
'Check if it is a NewRow action
If e.Action = DataRowAction.Add Then
Dim i As Integer
For i = 0 To (dataTable.Columns.Count - 1)
'Set the default values
dataTable.Columns(i).DefaultValue = e.Row(i)
Next
End If
End Sub
C E replied to Huggy Bear on 01-Feb-10 12:51 PM
Thanks for all the replies and thanks in particular to Huggy Bear
that gave me the good idea, as he said :-)

Here is my solution to the problem:

private void dataTable_RowChanged(object sender, System.Data.DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Add) 
            {
                int i;
                for (i = 0; i <= (dsData.Tables["MyDataTable"].Columns.Count - 1); i++) 
                {
                    if (dsData.Tables["MyDataTable"].Columns[i].ColumnName == "ID")
                    if (e.Row[i] == DBNull.Value)
                    {    
                        newValue++;
                        e.Row[i] = "NEW" + newValue.ToString().PadLeft(7,'0');
                    }
                }
            }
        }

Given that i only need the ID to be unique and dynamic, it works as expected.
Thank you very much again.

Regards,
C.