C# - 如何从数据源设置combobox datagridview的默认值?

时间:2016-06-27 09:32:15

标签: c# winforms datagridview combobox

我使用datasource在datagridview中添加了combobox。这是代码。

private void ExtractExcel(string filename)
    {
        allColumnName = allColumnName.Remove(allColumnName.Length - 1);
        string sqlMatchedData = "SELECT SOURCE_NAME, DESTINATION_NAME, DATA_TYPE, convert(integer, 100 * SIMILARITY) AS SIMILARITY " +
                                        "FROM TB_LOOKUP_COLUMN WHERE SOURCE_NAME IN (" + allColumnName + ") ORDER BY SIMILARITY ASC";
        ds = databaseManager.GetData(sqlMatchedData);

        dataGridView1.ColumnCount = 4;
        dataGridView1.Columns[0].Name = "SOURCE_NAME";
        dataGridView1.Columns[1].Name = "DESTINATION_NAME";
        dataGridView1.Columns[2].Name = "SIMILARITY(%)";
        dataGridView1.Columns[3].Name = "DATA_TYPE";

        foreach(DataRow row in ds.Tables[0].Rows)
        {
           int n = dataGridView1.Rows.Add();
           dataGridView1.Rows[n].Cells[0].Value = row[0].ToString();
           dataGridView1.Rows[n].Cells[2].Value = row[3].ToString();

           columnDestination = row[1].ToString();
           columnType = row[2].ToString();
           comboboxDestinationColumn(columnDestination, n);
           comboboxDataType(columnType, n);
        }

        dataGridView1.Rows[0].Cells[0].Selected = false;
        this.dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        this.dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        this.dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        this.dataGridView1.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        this.dataGridView1.Columns[0].ReadOnly = true;
    }


    private void comboboxDestinationColumn(string columnDestination, int n)
    {
        string sqlLookupColumn = "SELECT LookUpColumnID, SOURCE_NAME FROM TB_LOOKUP_COLUMN ORDER BY SOURCE_NAME ASC";
        DataSet dsColumn = databaseManager.GetData(sqlLookupColumn);

        DataGridViewComboBoxCell comboboxColumn = new DataGridViewComboBoxCell();
        comboboxColumn.Items.Add("Please Select");

        foreach (DataRow row in dsColumn.Tables[0].Rows)
        {
            comboboxColumn.Items.Add(row[1].ToString());
        }
        dataGridView1.Rows[n].Cells[1] = comboboxColumn;
    }

如果columnDestination是默认值,我不知道如何设置组合框的默认值。

我尝试使用DefaultValuesNeededCellFormatting,但我认为它不适合我的情况。

任何建议或代码示例都受到高度赞赏?

0 个答案:

没有答案