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.

Default value in ComboBox type in the grid

Thread ID:

Created:

Updated:

Platform:

Replies:

70724 Dec 27,2007 04:30 PM Dec 31,2007 01:27 PM Windows Forms 5
loading
Tags: GridControl
Yoni
Asked On December 27, 2007 04:30 PM

Hi,
I use GridDataBoundGrid.
I configure one of the gridBondClumns styleInfo.CellType to be "ComboBox". I also use the CurrentCellShowingDropDown event to show at every cell in the column other values.

I use code that is similar to this snippet:

private void grid_CurrentCellShowingDropDown(object sender, GridCurrentCellShowingDropDownEventArgs e)
{
GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;
if(cc.ColIndex == SpecifiecColumn)
{
GridComboBoxCellRenderer cr = cc.Renderer as GridComboBoxCellRenderer;
if(cr != null)
{
DataView dv = new DataView(ComboTable);
dv.RowFilter = string.Format("[masterId] = '{0}'", this.gridDataBoundGrid1[cc.RowIndex, MasterColumn].Text);
((GridComboBoxListBoxPart)cr.ListBoxPart).DataSource = dv;
}
}
}


My question is, how do I make the cells to show me the selected value (if there is no selected value then to show the first one) of their comboBox ?

Regards,
Yoni


haneefm [Syncfusion]
Replied On December 27, 2007 06:01 PM

Hi Yoni,

You could handle CurrentCellCloseDropDown and set the CellValue of the gridcell to ListBoxPart.SelectedItem at that point. Below are the codes:

private void grid_CurrentCellCloseDropDown(object sender, PopupClosedEventArgs e)
{
GridCurrentCell cc = this.grid.CurrentCell;
GridComboBoxCellRenderer cr = cc.Renderer as GridComboBoxCellRenderer;
if(cr != null)
{
if( cr.ListBoxPart.SelectedIndex > -1)
this.grid.Model[cr.RowIndex,cr.ColIndex].CellValue = cr.ListBoxPart.SelectedItem;
else
this.grid.Model[cr.RowIndex,cr.ColIndex].CellValue = cr.ListBoxPart.Items[0];///default value;
}
}

Best regards,
Haneef


Yoni
Replied On December 28, 2007 03:51 AM

My problem is that I want to show default values when the grid is initializing, and both of the events occur only after I click on the comboBox. How can I set the configuration for these cells to show default value, or to set the cells' dataSource (and valueMember and displayMember) while the grid is initializing and then to set the default value. I need that the vals in the others cols will be initialize first bacause I use their values.

thanks a lot,
Yoni


haneefm [Syncfusion]
Replied On December 28, 2007 01:42 PM

Hi Yoni,

In the grid, the default values come from GridDataBoundGrid.Model.ColStyles[col].CellValue. So, in your formload, if you have the DataTable.Columns[col].DefaultValue's, you could move them into the ColStyles. Below are the codes that shows how you might do it.

//set up things so grid uses the defaults...
for(int i = 0; i < dt.Columns.Count; ++i)
{
this.gridDataBoundGrid1.Model.ColStyles[i+1].CellValue = dt.Columns[i].DefaultValue;
}

But if you want to dynamically set the values without relying on the defaults in the DataTable, try handling CurrentCellMoved. The C# code for this would look like this:

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";
}

}

Let me know if you need more information.

Best Regards,
Haneef



Yoni
Replied On December 29, 2007 12:05 PM

I don't think that the "CurrentCellMoved" event will help me. I need to register to the cell initializing event, of there is something like thet ?
Because every cell has it's own dataSource and values, I need to configure at the beginning the datasource and valueMember and DisplayMember, and also to show at the beginning (the initializing of the cell) the default Value.

Can you help me ???

Regards,
Yoni


haneefm [Syncfusion]
Replied On December 31, 2007 01:27 PM

Hi Yoni,

One way you can do this by handling the QueryCellInfo event of the grid to show default value in a gridcell. Here is a code snippet that shows this task.

void gridDataBoundGrid1_CurrentCellCloseDropDown(object sender, Syncfusion.Windows.Forms.PopupClosedEventArgs e)
{
if (e.PopupCloseType == Syncfusion.Windows.Forms.PopupCloseType.Done)
{
GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;
hash.Add(GetCodeHash(cc.RowIndex, cc.ColIndex),"SelectedValue");
}
}

Hashtable hash = new Hashtable();
private Point GetCodeHash(int row, int col)
{
return new Point(row, col);
}

void Model_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
if (e.RowIndex > 0 && e.ColIndex > 0
&& (e.Style.Text == null || e.Style.Text == string.Empty)
&& !hash.Contains(GetCodeHash(e.RowIndex, e.ColIndex)))
{
e.Style.Text = e.Style.ChoiceList[0];
}
}

Please refer to the attached sample for implementation and let me know if this helps.
GDBG_DefaultValue.zip

Best regards,
Haneef


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.

;