BindingSource bs = new BindingSource();
private int datID; public Form2(int dID)
{
InitializeComponent();
dgv_ptitle.AutoGenerateColumns = false;
titleType.DataSource = tdc.getCVS("类型");
titleType.DisplayMember = "cvName";
titleType.ValueMember = "cvID";
datID = dID;
GridBind();
} private void GridBind()
{
List<PM_Title> pts = tdc.getTitles(datID);
if (pts.Count > 0)
{
bs.DataSource = pts;
bs.AllowNew = true;
dgv_ptitle.DataSource = bs;
}
}
在datagridview使用内建行添加数据。
1、没有绑定数据时,直接设置AllowUserToAddRows=true即可添加新行;没有问题。
2、直接绑定数据后(dgv_ptitle.DataSource = pts),设置AllowUserToAddRows=true无效,使用BindingSource绑定数据并设置bs.AllowNew = true;可在最后出现新行;没有问题。
3、在第二种情况下,如果表的列中有非DataGridViewTextBoxColumn列,如DataGridViewComboBoxColumn或自定义的日期选择列时,点击新行添加数据时报错,找不到DataGridViewComboBoxColumn的值,或把日期选择列当作TextBox,导致数据转换出错。
请问各位大侠有没有好的解决方案,网上暂时还找不到,大家都来讨论一下。
前提一定是在datagridview里内建新行。
private int datID; public Form2(int dID)
{
InitializeComponent();
dgv_ptitle.AutoGenerateColumns = false;
titleType.DataSource = tdc.getCVS("类型");
titleType.DisplayMember = "cvName";
titleType.ValueMember = "cvID";
datID = dID;
GridBind();
} private void GridBind()
{
List<PM_Title> pts = tdc.getTitles(datID);
if (pts.Count > 0)
{
bs.DataSource = pts;
bs.AllowNew = true;
dgv_ptitle.DataSource = bs;
}
}
在datagridview使用内建行添加数据。
1、没有绑定数据时,直接设置AllowUserToAddRows=true即可添加新行;没有问题。
2、直接绑定数据后(dgv_ptitle.DataSource = pts),设置AllowUserToAddRows=true无效,使用BindingSource绑定数据并设置bs.AllowNew = true;可在最后出现新行;没有问题。
3、在第二种情况下,如果表的列中有非DataGridViewTextBoxColumn列,如DataGridViewComboBoxColumn或自定义的日期选择列时,点击新行添加数据时报错,找不到DataGridViewComboBoxColumn的值,或把日期选择列当作TextBox,导致数据转换出错。
请问各位大侠有没有好的解决方案,网上暂时还找不到,大家都来讨论一下。
前提一定是在datagridview里内建新行。
像你现在做的这种我没有试过。。
学习中。。
DateTime报错不知道你日期选择列是怎么写的。
drw["用户编号"] = teUserID.Text.Trim();
drw["用户名称"] = teUserName.Text.Trim();
dtAdd.Rows.Add(drw);
1、对于文本列不用做处理。
2、对于下拉列表列,添加DataError事件,不用做任何处理。
3、日期选择列,参考 http://blog.csdn.net/lgh112437/archive/2010/01/27/5262122.aspx
这样就可以实现在DataGridView里面内建和修改数据了。感谢大家,散分