Orientos
Orientos

Reputation: 11

Binding DataGridViewComboBoxColumn to DataTable is not working

I am binding DataGridViewComboBoxColumn to DataTable, but the the cells of the column are not displaying the bound table. any idea? to test the DataTable, I bound it to a normal ComboBox and this shows the expected behavior.

 private void populateDataGW()
    {
        int addressesCount = data.Length / 186;
        TestingAdrress[] addressArray = new TestingAdrress[addressesCount];
        addressArray = getArray();
        DataGridViewRow dtRow = new DataGridViewRow();

        dtTableCmbBx = getAllCustomers();
        DataGridViewComboBoxColumn cmBxCol = new DataGridViewComboBoxColumn();
        //Binding here is not working
        cmBxCol.DataSource = dtTableCmbBx;
        cmBxCol.ValueMember = "Customer_ID";
        cmBxCol.DisplayMember = "Name";
        dtGrViTestAddress.Columns.Add(cmBxCol);
        //code for "normal" combobox
        comboBox1.DataSource = dtTableCmbBx;
        comboBox1.DisplayMember= "Name";
        comboBox1.ValueMember = "Customer_ID";

        DataGridViewButtonCell btnCell = new DataGridViewButtonCell();
        btnCell.Value = "Hinzufügen";

        for (int i=0; i< addressArray.Length;i++)
        {
            dtGrViTestAddress.Rows.Add();
            dtGrViTestAddress.Rows[i].Cells[0].Value = addressArray[i].Name;
            dtGrViTestAddress.Rows[i].Cells[1].Value = addressArray[i].Street;
            dtGrViTestAddress.Rows[i].Cells[2].Value = addressArray[i].PostalCode;
            dtGrViTestAddress.Rows[i].Cells[3].Value = addressArray[i].City;
            dtGrViTestAddress.Rows[i].Cells[5] = btnCell;                
        }
    }

Here is a screenshot:

enter image description here

Upvotes: 0

Views: 1206

Answers (2)

Ahsan
Ahsan

Reputation: 3

Method 1: Use Linq

var details = (from x in db.Details
               orderby x.Datetime descending
               where x.RaisedBy == "xyz"
               select x).ToList();

comboBox1.ValueMember ="id";
comboBox1.DataSource = details;

comboBox2.ValueMember ="Name";
comboBox2.DataSource = details;

comboBox3.ValueMember ="Street";
comboBox3.DataSource = details;

comboBox4.ValueMember ="PostalCode";
comboBox4.DataSource = details;

Method :2

var combocolumnA = new DataGridViewComboBoxColumn();
combocolumnA.HeaderText = "ID"; // grid header name
combocolumnA.ValueMember = "id";// database Column name
combocolumnA.DataSource = details;
GV.Columns.Add(combocolumnA);
combocolumnA.Width = 100;

var combocolumnB = new DataGridViewComboBoxColumn();
combocolumnB.HeaderText = "Name";
combocolumnB.ValueMember = "Name";
combocolumnB.DataSource = details;
GV.Columns.Add(combocolumnB);
combocolumnB.Width = 150;

Edit:

Just to tell you one more thing... If you want the data in grid view suppose when you select name from combo box then data automatically change all fields of gridview according to database ?

Upvotes: 0

Orientos
Orientos

Reputation: 11

i found the problem. i wanted the datagridview to be readOnly, so thats why the combobox didnt show the items in it, now i changed this property, and i set it for each column separated, and let the comboboxcolumn to be readOnly=false

Upvotes: 1

Related Questions