Click here to Skip to main content
15,303,572 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hey!
in a win forms application, i'm loading a datagridview called dg from a data source called servRet.
the servRet fills in the data grid. after setting the dg.datasource to servRet.value, i'm creating a new column of type DataGridViewComboBoxColumn which future to hold types of coins (e.g: CAD, US, NIS...).
Now, here is the tricky part:
the combo box suppose to display the displayValue from the servRet but when the client open the combo box, he needs to see all the coins list.
the coins list is dragged from another data source called servRet1.
i'm seek to accomplish a state where the combo box display the current value from the current row in the servRet and at the same time fill the item of the combo with the second data source.
unfortunately, i ran into several problems:
1. i tried to pass the servRet1 to a list and the load it to the combo but i cant add items to a binded combo box.
2. i cant load 2 data sources to one combo box.
3. even when loading the servRet to the combo box and on the drag open i'm setting the combo data source to null and load the servRet1, on the selectedIndexChange, it doesn't allow me to choose any other value rather then the first value it held.
here is the code for loading the first data:
C#
public void FillData(IShaarimService service)
{
    List<string> listTeuraMatbea = new List<string>();
    List<string> listKodAndShemMatbea = new List<string>();
    var servRet = service.getNiyarotBeMatbeaShoneMeBank();
    var servRet1 = service.getMatbeaot();

    if (ServiceUtil.IsValid(servRet1))
    {

        foreach (DataRow row in servRet1.Value.Rows)
        {
            listKodAndShemMatbea.Add(row[&quot;KodAndShemMatbea&quot;].ToString());
            listTeuraMatbea.Add(row[&quot;TeuraMatbea&quot;].ToString());
        }
    }

    if (ServiceUtil.IsValid(servRet))
    {
        grdNiyar.SetDataSourceByServerReturn(servRet);
        dg.AutoGenerateColumns = false;
        dg.ColumnCount = 4;
        dg.DataSource = servRet.Value;
        DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
        cb.DataSource = servRet.Value.DefaultView.ToTable(true, KodAndShemMatbea&);
        //foreach (var item in listKodAndShemMatbea)
        //{
        //    cb.Items.Add(item);
        //}
        cb.HeaderText = &quot;מטבע בבנק&quot;;
        cb.DisplayMember = &quot;123&quot;;
        cb.DataPropertyName = &quot;KodAndShemMatbea&quot;;
        cb.DisplayMember = &quot;KodAndShemMatbea&quot;;
        cb.ValueMember = &quot;KodAndShemMatbea&quot;;
        dg.Columns.Add(cb);

    }</pre>


any help we be appreciate!!!
Posted

1 solution

I think this MSDN example will help you forward
DataGridViewComboBoxColumn.DataSource Property[^]

Basically it works like this.
1. You have a DataGridView where you set the DataSource to a DataTable or BindingSource.
2. You specify that one of the columns should be a DataGridViewComboBoxColumn
This column has a property called DataPropertyName which is set to a column in the DataTable.

Code from the designer module
C#
this.dataSet1 = new System.Data.DataSet();
this.dataTable1 = new System.Data.DataTable();
this.dataColumn1 = new System.Data.DataColumn();
this.dataColumn2 = new System.Data.DataColumn();
this.bindingSource3 = new System.Windows.Forms.BindingSource(this.components);
this.columnSeasons = new System.Windows.Forms.DataGridViewComboBoxColumn();

this.dataGridView1.DataSource = this.bindingSource3;

// 
// dataSet1
// 
this.dataSet1.DataSetName = "NewDataSet";
this.dataSet1.Tables.AddRange(new System.Data.DataTable[] {
this.dataTable1});
// 
// dataTable1
// 
this.dataTable1.Columns.AddRange(new System.Data.DataColumn[] {
this.dataColumn1,
this.dataColumn2});
this.dataTable1.TableName = "Table1";
// 
// dataColumn1
// 
this.dataColumn1.ColumnName = "Column1";
// 
// dataColumn2
// 
this.dataColumn2.ColumnName = "Column2";
// 
// bindingSource3
// 
this.bindingSource3.DataMember = "Table1";
this.bindingSource3.DataSource = this.dataSet1;
// 
// columnSeasons
// 
this.columnSeasons.DataPropertyName = "Column1"; // This comes from the DataSet
this.columnSeasons.HeaderText = "Seasons";
this.columnSeasons.Name = "columnSeasons";
this.columnSeasons.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.columnSeasons.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;



3. Now you need to set the data source for the dropdown list of the combo box.
This can be a list of strings or a data table.
C#
DataTable dtSeasons = new DataTable("Seasons");
dtSeasons.Columns.Add("Id", typeof(int));
dtSeasons.Columns.Add("Value", typeof(string));

dtSeasons.Rows.Add(1, "Winter");
dtSeasons.Rows.Add(2, "Spring");
dtSeasons.Rows.Add(3, "Summer");
dtSeasons.Rows.Add(4, "Autumn");

columnSeasons.DataSource = dtSeasons;
columnSeasons.ValueMember = "Id";
columnSeasons.DisplayMember = "Value";


The code sample is not complete but I hope you get the picture.
   
v3
Comments
oron sultan 1-Dec-15 3:20am
   
Dear George,
thanks for the quick reply, although i cant see how your answer solve my problem :-)
i already visit the link u gave me during my research.
once again, i have 2 different datasource. on the combobox text i need to show data taken from datasource1 and on the drop down list i need to show data taken from datasource2. what u gave me is a way to handle one datasource.
please, if u can explain your answer i will be thankful.
George Jonsson 1-Dec-15 3:26am
   
It is actually two different data sources, but maybe I wasn't clear enough.
I thought you could understand the principle and go from there.
I will update my solution with foolproof code.
oron sultan 1-Dec-15 3:23am
   
btw, the DataGridViewComboBoxColumn is created on runtime in order to set its datasource.
George Jonsson 1-Dec-15 3:24am
   
You don't need to do that. You can create the column at design time and then just change the data source.
oron sultan 1-Dec-15 4:21am
   
ok, first of all thanks a lot!
but i still have one question... i cant see why i need the definition for the dataset, datatable and datacolumns if eventually u create a datatable called "dtSeasons"?
and another thing: how can i display the current value from the datasource for each combobox in each row? sorry for digging...
oron sultan 1-Dec-15 4:28am
   
i cant see where u load the value from datasource1 and where you load the list from datasource2 for the dropdown...
George Jonsson 1-Dec-15 4:43am
   
I just tried to make a valid example.

You should look into how to fill a DataSet from your database or whatever it is you have in the background.
Or fill the data into two different tables, one that you show in the DataGridView and one you use for the dropdown list.

Then the bind DataTable1 to the DataGridView and will show all the values in the data table.

The second data table, dtSeasons in my example, you use for the combo box.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900