[英]C# - How to populate combobox in datagridview only in certain column?
I have a requirement to populate data in gridview as below.我需要在 gridview 中填充数据,如下所示。
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.您必须从 GridView 检查RowDataBound
并在那里分配一个值。
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.我建议订阅DataGridView.DefaultValuesNeeded
并为每行中的每个ComboBox
设置默认值。
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.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.