我需要在gridview中填充数据,如下所示。 我有一个存储所有这个值的表。对于每个“目标列”和“类型”将根据“源列”分配值。但我在设置“目标列”和“类型”的默认值时遇到问题。下面是代码。有人可以给我一个想法。因为我整天坚持这一点。任何帮助都非常感谢。提前谢谢。
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";
答案 0 :(得分:0)
您可以查看this article。您必须从GridView中检查RowDataBound
并在那里分配值。
这是一个例子:
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();
}
}
}
答案 1 :(得分:0)
我建议,订阅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.
}