关于DataGridViewComboBoxColumn的赋值问题!!

hf26snowolf 2009-07-03 12:37:41
我是先将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;
}

...全文
376 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
kangbo818 2009-07-07
  • 打赏
  • 举报
回复
hf26snowolf 2009-07-07
  • 打赏
  • 举报
回复
顶上来!!
hf26snowolf 2009-07-06
  • 打赏
  • 举报
回复
。。。。。。。
hf26snowolf 2009-07-05
  • 打赏
  • 举报
回复
再顶
hf26snowolf 2009-07-04
  • 打赏
  • 举报
回复
那是不是我只能屏掉它的排序,然后自己写一个?
ootwooo 2009-07-04
  • 打赏
  • 举报
回复
up
hf26snowolf 2009-07-04
  • 打赏
  • 举报
回复
顶上来!!
surlew 2009-07-03
  • 打赏
  • 举报
回复

//尺码绑定
DataGridViewComboBoxColumn List尺码 = (DataGridViewComboBoxColumn)grd.Columns["尺码"];
string strSql尺码 = "Select * From JC款式_尺码组名称 Where 款式编号 = '" + 款式编号 + "' ";

DataSet ds尺码 = clsSql.GetDs(strSql尺码);

List尺码.Items.Clear();
for (int i = 0; i < ds尺码.Tables[0].Rows.Count; i++)
{
for (int n = 0; n <= 11; n++)
{
if (ds尺码.Tables[0].Rows[i][n + 1].ToString() != "")
{
List尺码.Items.Add(ds尺码.Tables[0].Rows[i][n + 1].ToString());
}
}
}
ds尺码.Dispose();
yan_pu 2009-07-03
  • 打赏
  • 举报
回复
DataGridView赋值之后数据库里面更改了么?可能是你点击页头DataGridView就重新绑定了一下数据源的原因
hf26snowolf 2009-07-03
  • 打赏
  • 举报
回复
楼上的和我的不一样啊!!

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧