关于DataGridViewComboBoxColumn的赋值问题!!

hf26snowolf hf26snowolf 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我是先将DataGridView绑了数据,然后用绑定数据的一个ComboBox列替换了原来对应字段的TEXTBOX列,但是在赋值时遇到了问题,如红字体,赋完值后,我随便点了一下“ID”和“Position”的列头,在该列排序的同时中间的两个ComboBox列原本已选好的值全部都消失了,如下图,我原以为是临时行的问题,就换了如绿体的方法,但是效果依旧,谁知道这个问题如何解决?

public bool columns_replace(string str_sql, string str_field)
{
DataSet ds = new DataSet();
ds = database_link(str_sql);
if (ds.Tables[0].Rows.Count == 0)
{
MessageBox.Show("该字段信息不存在!");
return false;
}
DataGridViewComboBoxColumn dg = new DataGridViewComboBoxColumn();
dg.DataSource = ds.Tables[0];
dg.ValueMember = "ID";
dg.DisplayMember = "Name";

int int_index = dgv_edit.Columns[str_field].Index;
dgv_edit.Columns.Insert(int_index, dg);
foreach (DataGridViewRow dr in dgv_edit.Rows)
{
dr.Cells[int_index].Value = dr.Cells[int_index + 1].Value;
}

//for (int int_row = 0; int_row < dgv_edit.Rows.Count; int_row++)
//{
// dgv_edit.Rows[int_row].Cells[int_index].Value = dgv_edit.Rows[int_row].Cells[int_index+1].Value;
//}

dgv_edit.Columns.Remove(str_field);
dgv_edit.Columns[int_index].Name = str_field;
dgv_edit.Columns[int_index].HeaderText = str_field;

return true;
}

...全文

相关推荐

评论 10
收藏 已收藏
分享
返回顶部
hf26snowolf
C# 111,129 成员 642,540 内容