gpt4 book ai didi

c# - 填充数据 GridView 组合框

转载 作者:太空狗 更新时间:2023-10-30 01:05:12 25 4
gpt4 key购买 nike

我的 Windows 窗体加载中有以下代码:

    private void Panou_Load(object sender, EventArgs e)
{

List<string>[] list;

//list in a array with all elements from a select query
list = Conexiune.Select();


dataGridView1.Rows.Clear();

for (int i = 0; i < list[0].Count; i++)
{
int number = dataGridView1.Rows.Add();
dataGridView1.Rows[number].Cells[0].Value = list[0][i];
dataGridView1.Rows[number].Cells[1].Value = list[1][i];
dataGridView1.Rows[number].Cells[2].Value = list[2][i];

dataGridView1.Rows[number].Cells[4].Value = list[4][i];
dataGridView1.Rows[number].Cells[5].Value = list[5][i];
dataGridView1.Rows[number].Cells[6].Value = list[6][i];
}
}

在我的数据 GridView 中,第 4 个单元格是一个组合框。如何使用我选择的值(list[3][i] 变量)填充组合框?

更新 1:

    private void Panou_Load(object sender, EventArgs e)
{

List<string>[] list;

list = Conexiune.Select();
dataGridView1.Rows.Clear();


for (int i = 0; i < list[0].Count; i++)
{
int number = dataGridView1.Rows.Add();
dataGridView1.Rows[number].Cells[0].Value = list[0][i];
dataGridView1.Rows[number].Cells[1].Value = list[1][i];
dataGridView1.Rows[number].Cells[2].Value = list[2][i];
(dataGridView1.Columns[3] as DataGridViewComboBoxColumn).DataSource = new List<string> { list[3][i] };
dataGridView1.Rows[number].Cells[4].Value = list[4][i];
dataGridView1.Rows[number].Cells[5].Value = list[5][i];
dataGridView1.Rows[number].Cells[6].Value = list[6][i];
}
}

http://i.stack.imgur.com/0WLDr.jpg

更新 2:

    private void Panou_Load(object sender, EventArgs e)
{

List<string>[] list;

list = Conexiune.Select();
dataGridView1.Rows.Clear();

(dataGridView1.Columns[3] as DataGridViewComboBoxColumn).DataSource = new List<string> { "", "activ", "inactiv", "neverificat" };

for (int i = 0; i < list[0].Count; i++)
{
int number = dataGridView1.Rows.Add();
dataGridView1.Rows[number].Cells[0].Value = list[0][i];
dataGridView1.Rows[number].Cells[1].Value = list[1][i];
dataGridView1.Rows[number].Cells[2].Value = list[2][i];
dataGridView1.Rows[number].Cells[3].Value = list[3][i];
dataGridView1.Rows[number].Cells[4].Value = list[4][i];
dataGridView1.Rows[number].Cells[5].Value = list[5][i];
dataGridView1.Rows[number].Cells[6].Value = list[6][i];
}
}

http://i.stack.imgur.com/MlnER.jpg

最佳答案

为组合框列设置数据源

(dataGridView1.Columns[0] as DataGridViewComboBoxColumn).DataSource 
= new List<string> { "Apples", "Oranges", "Grapes"};

for (int i = 0; i < list[0].Count; i++)
{
int number = dataGridView1.Rows.Add();
dataGridView1.Rows[number].Cells[0].Value = list[3][i]; //list[3][1]=="Apples"
}
}

关于c# - 填充数据 GridView 组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19941569/

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