简体   繁体   中英

C# - How to populate combobox in datagridview only in certain column?

I have a requirement to populate data in gridview as below.

在此处输入图片说明

I have a table which stores all of this value. For each "Destination Column" and "Type" will be assigned value based on "Source Column". But I have a problem in setting the default value for "Destination Column" and "Type". Can someone give me an idea. As I have been stuck with this for whole day. Any help is highly appreciated.

Below is the code.

string sqlMatchedData = "SELECT convert(integer, 100 * SIMILARITY) AS SIMILARITY, SOURCE_NAME " + 
                        "FROM TB_LOOKUP_COLUMN WHERE SOURCE_NAME IN (" + allColumnName + ")";
ds = databaseManager.GetData(sqlMatchedData);
//dataGridView1.DataSource = ds.Tables[0];
//dataGridView1.Columns[1].Visible = false;
//dataGridView1.Columns[2].Visible = false;

dt = new DataTable();
DataRow dr;
dt.Columns.Add("Similarity (%)");
dt.Columns.Add("Source Column");
//dt.Columns.Add("C");
//dt.Columns.Add("D");

foreach (DataRow row in ds.Tables[0].Rows)
{
    dr = dt.NewRow();
    string v = row[0].ToString();
    string v1 = row[1].ToString();
    //string v2 = row[2].ToString();
    //string v3 = row[3].ToString();

    dr[0] = v;
    dr[1] = v1;
    //dr[2] = v2;
    //dr[3] = v3;
    dt.Rows.Add(dr);
}
dataGridView1.DataSource = dt;

//LookupColumn
string sqlLookupColumn = "SELECT LookUpColumnID, SOURCE_NAME FROM TB_LOOKUP_COLUMN";
DataSet dsColumn = databaseManager.GetData(sqlLookupColumn);
DataGridViewComboBoxColumn comboLookup = new DataGridViewComboBoxColumn();
comboLookup.DataSource = dsColumn.Tables[0];
comboLookup.HeaderText = "Destination Column";
comboLookup.Name = "Destination";
comboLookup.DisplayMember = "SOURCE_NAME";
comboLookup.ValueMember = "LookUpColumnID";
dataGridView1.Columns.Add(comboLookup);
comboLookup.DefaultCellStyle.NullValue = "Please Select";

//datatype
string sqlDataType = "SELECT DataTypeLookUpID, DATATYPE FROM TB_LOOKUP_DATATYPE";
DataSet dsDataType = databaseManager.GetData(sqlDataType);
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.DataSource = dsDataType.Tables[0];
combo.HeaderText = "Type";
combo.Name = "Type";
combo.DisplayMember = "DATATYPE";
combo.ValueMember = "DataTypeLookUpID";
dataGridView1.Columns.Add(combo);
combo.DefaultCellStyle.NullValue = "Please Select";

You can check this article . You have to check RowDataBound from your GridView and assign a value there.

And here is the example:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType== DataControlRowType.DataRow)
    {
        DataRowView drv = e.Row.DataItem as DataRowView;
        DropDownList ddlCategories = e.Row.FindControl("DdlCategories") as DropDownList;
        if(ddlCategories != null)
        {
            //Get the data from DB and bind the dropdownlist
            ddlCategories.SelectedValue = drv["CategoryID"].ToString();
        }
    }
}

I would suggest, subscribe for DataGridView.DefaultValuesNeeded and set the default value for each ComboBox in a row.

dataGridView1.DefaultValuesNeeded  += dataGridView1_DefaultValuesNeeded;

private void dataGridView1_DefaultValuesNeeded(object sender,
    System.Windows.Forms.DataGridViewRowEventArgs e)
{
     e.Row.Cells["Destination Column"].Value = "any value"; any default value.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM