gpt4 book ai didi

c# - DataGridViewComboBoxCell 数据源为空

转载 作者:太空宇宙 更新时间:2023-11-03 15:02:27 24 4
gpt4 key购买 nike

我有一个 DataGridView,其中有一列作为 ComboBox这是我用来创建列然后为每一行更新 DataSource 的代码。

var dTable = new DataTable();
dTable.Columns.Add("Id",typeof(int));
dTable.Columns.Add("Desc", typeof(string));

for (int i = 0; i < 10; i++)
{
var dRow = dTable.NewRow();

dRow[0] = i;
dRow[1] = "test";
dTable.Rows.Add(dRow);
}
dataGridView1.DataSource = dTable;

//Create the ComboBoxColumn at the end of the grid
var cmb = new DataGridViewComboBoxColumn();
cmb.Name = "ComboCol";
cmb.HeaderText = "ComboCol";

dataGridView1.Columns.Add(cmb);

UpdateDataSourceCombo();

private void UpdateDataSourceCombo()
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
var comboCell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells["ComboCol"];
//Same datasource for every row just for testing
comboCell.DataSource = new string[] { "a", "b", "c" };
}
}

该列已正确创建为 ComboBox,但它始终为空。

更新经过进一步测试,我知道只有当我为整个 DGV 使用数据源时才会出现问题。

如果我自己创建列,效果很好:

var test = new DataGridViewTextBoxColumn();
test.Name = "asd";
dataGridView1.Columns.Add(test);
dataGridView1.Rows.Add(new DataGridViewRow());

var cmb = new DataGridViewComboBoxColumn();
cmb.Name = "ComboCol";
cmb.HeaderText = "ComboCol";

dataGridView1.Columns.Add(cmb);

UpdateDataSourceCombo(); //Same function as the original post

我用将数据表加载到网格中的示例更新了原始代码。

最佳答案

像这样处理我的项目:为源数组创建一个类。设置组合框列的 DisplayMemberPath 和 ValueMemberPath 属性。

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sof
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
System.Data.DataTable dTable = new DataTable();
using (System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection("Data Source=.;Initial Catalog=testBase;Integrated security=true"))
{
using (System.Data.SqlClient.SqlDataAdapter sqlAdp = new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM City WHERE City_Name LIKE 'Chalon%'", sqlConn))
{
sqlConn.Open();
sqlAdp.Fill(dTable);
}
}

dataGridView1.DataSource = dTable;

//Create the ComboBoxColumn at the end of the grid
var cmb = new DataGridViewComboBoxColumn();
cmb.Name = "ComboCol";
cmb.HeaderText = "ComboCol";


cmb.DisplayMember = "Display";
cmb.ValueMember = "Value";
dataGridView1.Columns.Add(cmb);

UpdateDataSourceCombo();


}
private void UpdateDataSourceCombo()
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
var comboCell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells["ComboCol"];
//Same datasource for every row just for testing
if (i % 2 == 0)
comboCell.DataSource = new customObj[] { new sof.customObj("a", "a"), new sof.customObj("b", "b"), new customObj("c", "c") };
else
comboCell.DataSource = new customObj[] { new sof.customObj("1", "1"), new sof.customObj("2", "2"), new customObj("3", "3") };
}
}

}

class customObj
{
public string Value { get; set; }
public string Display { get; set; }
public customObj(string value, string display)
{
this.Value = value;
this.Display = display;
}

}
}

关于c# - DataGridViewComboBoxCell 数据源为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45488470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com