We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

Is there a DefaultValuesNeeded

Thread ID:

Created:

Updated:

Platform:

Replies:

76128 Aug 24,2008 12:07 PM Sep 16,2008 05:42 AM Windows Forms 7
loading
Tags: GridControl
Detlef Birkholz
Asked On August 24, 2008 12:07 PM

I have a application which uses at the moment the original datagridview from .net. Now I want to use a GridDataBoundGrid, but I have a problem, I need the DefaultValuesNeeded event to populate a new record with some default values. How can this be done with a GridDataBoundGrid?

Yours
Detlef


Jisha Joy [Syncfusion]
Replied On August 26, 2008 12:44 AM

Hi Detlef,

We appreciate your interest in Syncfusion products.

This can be done by setting the values in the RowEnter event and also by handling RowEditing and RowLeave events of GridDataBoundGrid Control.

Below is the code snippet:


private void gridDataBoundGrid1_RowEnter(object sender, Syncfusion.Windows.Forms.Grid.GridRowEventArgs e)
{
if (e.RowIndex == this.gridDataBoundGrid1.Model.RowCount)
{
this.gridDataBoundGrid1.CurrentCell.BeginEdit(true);
for (int i = 0; i < dt.Columns.Count; i++)
{
this.gridDataBoundGrid1.Model.ColStyles[i + 1].Text = (string)dt.Columns[i].DefaultValue;
}
}
}

private void gridDataBoundGrid1_RowEditing(object sender, Syncfusion.Windows.Forms.Grid.GridRowEventArgs e)
{
if (this.gridDataBoundGrid1.Binder.IsAppendRow)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
this.gridDataBoundGrid1.Model.ColStyles[i + 1].Text = "";
}
}
}

private void gridDataBoundGrid1_RowLeave(object sender, Syncfusion.Windows.Forms.Grid.GridRowEventArgs e)
{
if (this.gridDataBoundGrid1.Binder.IsAppendRow)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
this.gridDataBoundGrid1.Model.ColStyles[i + 1].Text = "";
}
}
}


The grid gets these default values from the Model.ColStyles[col].Text property. The idea is to conditionally set these value only when you enter the append row, and to reset them to blank when you start to edit the append row or when you leave the append row. If you set them earlier, or leave them set, then the append row will alway show the defaults (and never be blank).


You can get the sample here:

'>http://websamples.syncfusion.com/samples/Grid.Windows/Grid_WF_GDBGDefaultvalues/main.htm">
http://websamples.syncfusion.com/samples/Grid.Windows/Grid_WF_GDBGDefaultvalues/main.htm

Please let me know if this helps.

Regards,
Jisha




Detlef Birkholz
Replied On August 26, 2008 03:22 AM

Hi Jisha,

thanks for your example. It works, but only if all of the table columns are also display in the grid, but unfortunately some of my table columns that need to have default values, aren't displayed in the grid.

Can you give me some hints, who to acomplish this task?

Yours
Detlef


Jisha Joy [Syncfusion]
Replied On August 29, 2008 05:20 AM

Hi Detlef,

Thank you for your update.

I am not able to understand your requirements clearly from your explanation. Could you please explain it in detail. So that we could sort out the cause of the issue and provide you a solution soon.

We appreciate your interest in Syncfusion products.

Regards,
Jisha


Detlef Birkholz
Replied On September 9, 2008 01:11 PM

Hi Jisha,

my problem is as follows: I have a GridBoundDataGrid that is bound to a database. The grid displays only a part of the database (i.e. not all database fields have corresponding columns in the grid).
When I add a new row I want the fields that are not displayed to be filled with some "default values" (these default values are depending from some variables in the program so they're not fixed).

With your sample I can only set default values for database fields that are displayed in the grid.

The .NET DataViewGrid has an event called DefaultValuesNeeded where I can set these "default values" in the underlying datasource. Now I'm looking for something similar for the GridDataBoundGrid.

Regards,
Detlef


Jisha Joy [Syncfusion]
Replied On September 10, 2008 07:43 AM

Hi Detlef,

Thank you for your update.

If you want to dynamically set the values without relying on the defaults in the DataTable, try handling CurrentCellMoved.


Here is the code:



private void gridDataBoundGrid1_CurrentCellMoved(object sender, Syncfusion.Windows.Forms.Grid.GridCurrentCellMovedEventArgs e)

{

GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;

if(cc.RowIndex == this.gridDataBoundGrid1.Model.RowCount)

{

this.gridDataBoundGrid1.Binder.BeginEdit();

this.gridDataBoundGrid1[cc.RowIndex , 1].Text = "defaultcol1";

this.gridDataBoundGrid1[cc.RowIndex , 2].Text = "defaultcol2";

}

}




Please let me know if this helps.

Regards,
Jisha


Detlef Birkholz
Replied On September 10, 2008 10:19 AM

Hi Jisha,

thanks for the sample.

But how do I handle the following situation:

I have a database with four fields A,B,C and D. In my datagrid I display only two fields A and B.

Now the user enters a new row fills in the values for A and B. Then the program must set the values for the fields C and D to some default values in the underlying datasource.

Can you help me with this situation?

BTW: The whole scenario plays with LINQ, so there is no datatable, only entities defined by LINQToSQL.

Regards
Detlef


Jisha Joy [Syncfusion]
Replied On September 16, 2008 05:42 AM

Hi Detlef,

Thank you for your update.

This can be achieved by CurrentCellMoved event. Please refer the sample for more details.

http://www.syncfusion.com/support/user/uploads/GDBGDefaultValuesNeededEvent_bedb6a0d.zip


Thanks,
Jisha


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;