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. Image for the cookie policy date

Default value in ComboBox type in the grid

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


5 Replies

HA haneefm Syncfusion Team December 27, 2007 11:01 PM UTC

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



YO Yoni December 28, 2007 08:51 AM UTC

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



HA haneefm Syncfusion Team December 28, 2007 06:42 PM UTC

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




YO Yoni December 29, 2007 05:05 PM UTC

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



HA haneefm Syncfusion Team December 31, 2007 06:27 PM UTC

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


Loader.
Live Chat Icon For mobile
Up arrow icon