Question

Using C#

Using SQL I populate a datatable with the results from a query.

then I created a datagrid that uses that datatable as its source.

The datagrid is already configured with a column of type DataGridViewComboBoxColumn.

The Combobox's datasource is another SQL loaded datatable that has two strings for each row. One string is the actual value I need, the second string is the user visible string.

The combobox is populated correctly with the information from the 2nd datatable and the values work as desired. When I save the data out of the datatable I'm getting the correct information for the combobox.

The problem comes into play when I try loading the saved SQL information back into that datagrid. The value doesn't seem to get bound to item the datatable.

Datagrid definition:

        dgHeaders.DataSource = dtCSVHeaders;

        dgHeaders.Columns["nIndex"].Visible = false;

        if (!dgHeaders.Columns.Contains("colType"))
        {
            DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn();
            colType.HeaderText = "DB Field";
            colType.DropDownWidth = 140;
            colType.Width = 140;
            colType.DataSource = dtFields;
            colType.DataPropertyName = "szDBField";
            colType.DisplayMember = "szVisualField";
            colType.ValueMember = "szDBField";
            colType.Name = "DB Field";
            colType.DefaultCellStyle.NullValue = "--Select--";
            dgHeaders.Columns.Add(colType);

        }

First I tried to set the column name colType to the value I was populating in the dataRow but that didn't seem to work.

Since I couldn't figure out how to get the databinding to work, I decided I'd try to force the cell to the value I wanted so I tried this on the dataload.

        ... SQL load

        foreach (DataRow drRow in dtLoad.Rows)
        {
            string szColumnName = drRow["szOrgColumnName"].ToString();
            string szMatchName = drRow["szMatchColumn"].ToString();
            DataRow drAdd = dtCSVHeaders.NewRow();
            drAdd["nIndex"] = nLoop;
            drAdd["szHeader"] = szColumnName;
            dtCSVHeaders.Rows.Add(drAdd);
            dgHeaders.Rows[nLoop].Cells[2].Value = szMatchName;
            nLoop++;
        }

But sadly that still doesn't set the value of the combobox.

I've got no errors or warnings on this code.

I'd prefer to let databinding take control and do its thing without me specifically setting the cell with the value. But if that's what I need to do then so be it...

Était-ce utile?

La solution

Hmm, your code (as far as I seen) seem good. I dont know what exactly is your "dtFields" object, but it should be dataTable. I did a simple example of how it should be done. Check it out:

public partial class Form1 : Form
{        
    public Form1()
    {
        InitializeComponent();
        dataGridView1.Columns.Add("column1", "Column name");
        dataGridView1.Columns.Add(CreateComboBox());
        //adding some rows:
        dataGridView1.Rows.Add("a");
        dataGridView1.Rows.Add("b");
    }

    private DataGridViewComboBoxColumn CreateComboBox()
    {
        DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
        {
            combo.Name = "comboColumn";
            combo.HeaderText = "Selection";
            combo.DataSource = GetDataForComboBox();
            combo.DisplayMember = "Name";
            combo.ValueMember = "Id";
        }
        return combo;
    }

    private DataTable GetDataForComboBox()
    {
        //this is your method to get the data from database

        //in my exmaple I will simply add some example data:
        DataTable table = new DataTable("ExampleData");
        table.Columns.Add("Id", typeof(int));
        table.Columns.Add("Name", typeof(string));
        table.Rows.Add(1, "Name 1");
        table.Rows.Add(2, "Name 2");
        table.Rows.Add(3, "Name 3");
        return table;
    }
}

This code works, and try to do the same, you only fill dataTable from dataBase. But if you want you can only try this exact code of mine, and you will see it work!

bye

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top