在DBGRIDEH网格中我想在(数量)这个字段中输入 39+20 然后数量自动变为59 但是现有的DBGRIDEH会出错的,请问高手如何解决 可以自己定义一个dataGrid的列,重写里面的textBox的方法或事件来实现. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 类的定义参考如下:public class TextColumnStyle : System.Windows.Forms.DataGridTextBoxColumn{ public TextColumnStyle() { // // TODO: 在此处添加构造函数逻辑 // } protected override bool Commit(System.Windows.Forms.CurrencyManager dataSource, int rowNum) { // TODO: 添加 TextColumnStyle.Commit 实现 string[] s = this.TextBox.Text.Split( new char[]{'+'}); if (s != null && s.Length ==2 ) { int v = int.Parse(s[0])+int.Parse(s[1]); SetColumnValueAtRow(dataSource, rowNum, v); } return base.Commit (dataSource, rowNum); }}使用时类似如下的代码:public void SetListDataEX(DataView dvw){ if (tableStyle==null) { tableStyle = new DataGridTableStyle(); tableStyle.MappingName =dvw.Table.TableName; int numCols = dvw.Table.Columns.Count; SqlDataServer.TextColumnStyle textColumn; for (int i=0;i<dvw.Table.Columns.Count;i++) { textColumn = new SqlDataServer.TextColumnStyle(); textColumn.NullText=""; textColumn.HeaderText = dvw.Table.Columns[i].Caption; textColumn.MappingName = dvw.Table.Columns[i].ColumnName; tableStyle.GridColumnStyles.Add(textColumn); } tableStyle.AllowSorting=false; tableStyle.RowHeadersVisible=false; this.dataGrid1.TableStyles.Clear(); this.dataGrid1.TableStyles.Add(tableStyle); } this.dataGrid1.BeginInit(); this.dataGrid1.SetDataBinding(dvw,null); this.dataGrid1.EndInit(); dvw.AllowDelete=true; dvw.AllowEdit=true; m_ListDataView=dvw;}你自己再改一下就OK了! 改了一下,这是一个较完整的例子了:public class TextColumnStyle : System.Windows.Forms.DataGridTextBoxColumn{ public TextColumnStyle() { // // TODO: 在此处添加构造函数逻辑 // } protected override bool Commit(System.Windows.Forms.CurrencyManager dataSource, int rowNum) { // TODO: 添加 TextColumnStyle.Commit 实现 /// 类似20+30的输入提交时自动算为50的列类型; string[] s = this.TextBox.Text.Split( new char[]{'+'}); if (s != null && s.Length ==2 ) { int v = int.Parse(s[0])+int.Parse(s[1]); SetColumnValueAtRow(dataSource, rowNum, v); } return base.Commit (dataSource, rowNum); }}DataGridTableStyle tableStyle =null;public void SetListDataEX(DataView dvw){ if (tableStyle==null) { tableStyle = new DataGridTableStyle(); tableStyle.MappingName =dvw.Table.TableName; int numCols = dvw.Table.Columns.Count; TextColumnStyle textColumn; for (int i=0;i<dvw.Table.Columns.Count;i++) { textColumn = new TextColumnStyle(); textColumn.ReadOnly=false; textColumn.NullText=""; textColumn.HeaderText = dvw.Table.Columns[i].Caption; textColumn.MappingName = dvw.Table.Columns[i].ColumnName; tableStyle.GridColumnStyles.Add(textColumn); } tableStyle.AllowSorting=false; tableStyle.RowHeadersVisible=false; this.dataGrid1.TableStyles.Clear(); this.dataGrid1.TableStyles.Add(tableStyle); } this.dataGrid1.BeginInit(); this.dataGrid1.SetDataBinding(dvw,null); this.dataGrid1.EndInit(); dvw.AllowDelete=true; dvw.AllowEdit=true;}private void button1_Click(object sender, System.EventArgs e){ string strConn="server=(local);database=mydata;uid=sa;pwd=sa"; SqlConnection cn=new SqlConnection(strConn); SqlDataAdapter ada=new SqlDataAdapter(); ada.SelectCommand=new SqlCommand("select * from system_personnel",cn); DataSet ds=new DataSet(); ada.Fill(ds); this.SetListDataEX(ds.Tables[0].DefaultView);} 怎么开发一个桌面显示天气的插件或软件 请各位用过castle 的高手帮助解决,先谢谢了 gridview导出数据为pdf格式 子form如何嵌入MDI的SplitContainer的右边panel 如何模拟鼠标点击C#自身窗体的指定位置并且窗体不获得焦点? 什么是构造函数和方法的重载,有没有区别 奇怪!升级VS2005后发现 DevExpress V3 组件的EditValue属性不能绑定 高分再求freetextbox 1.62版 怎样新开个窗口? 高手帮忙看看 关于DataGrid的一个很难的问题!! 初学C#,问个简单的问题大家来拿分吧!
public class TextColumnStyle : System.Windows.Forms.DataGridTextBoxColumn
{
public TextColumnStyle()
{
//
// TODO: 在此处添加构造函数逻辑
//
} protected override bool Commit(System.Windows.Forms.CurrencyManager dataSource, int rowNum)
{
// TODO: 添加 TextColumnStyle.Commit 实现 string[] s = this.TextBox.Text.Split( new char[]{'+'});
if (s != null && s.Length ==2 )
{
int v = int.Parse(s[0])+int.Parse(s[1]);
SetColumnValueAtRow(dataSource, rowNum, v);
}
return base.Commit (dataSource, rowNum);
}
}
使用时类似如下的代码:
public void SetListDataEX(DataView dvw)
{
if (tableStyle==null)
{
tableStyle = new DataGridTableStyle();
tableStyle.MappingName =dvw.Table.TableName;
int numCols = dvw.Table.Columns.Count;
SqlDataServer.TextColumnStyle textColumn;
for (int i=0;i<dvw.Table.Columns.Count;i++)
{
textColumn = new SqlDataServer.TextColumnStyle();
textColumn.NullText="";
textColumn.HeaderText = dvw.Table.Columns[i].Caption;
textColumn.MappingName = dvw.Table.Columns[i].ColumnName;
tableStyle.GridColumnStyles.Add(textColumn);
} tableStyle.AllowSorting=false;
tableStyle.RowHeadersVisible=false;
this.dataGrid1.TableStyles.Clear();
this.dataGrid1.TableStyles.Add(tableStyle);
}
this.dataGrid1.BeginInit();
this.dataGrid1.SetDataBinding(dvw,null);
this.dataGrid1.EndInit();
dvw.AllowDelete=true;
dvw.AllowEdit=true;
m_ListDataView=dvw;
}
你自己再改一下就OK了!
public class TextColumnStyle : System.Windows.Forms.DataGridTextBoxColumn
{
public TextColumnStyle()
{
//
// TODO: 在此处添加构造函数逻辑
//
} protected override bool Commit(System.Windows.Forms.CurrencyManager dataSource, int rowNum)
{
// TODO: 添加 TextColumnStyle.Commit 实现 /// 类似20+30的输入提交时自动算为50的列类型; string[] s = this.TextBox.Text.Split( new char[]{'+'});
if (s != null && s.Length ==2 )
{
int v = int.Parse(s[0])+int.Parse(s[1]);
SetColumnValueAtRow(dataSource, rowNum, v);
}
return base.Commit (dataSource, rowNum);
}
}DataGridTableStyle tableStyle =null;
public void SetListDataEX(DataView dvw)
{
if (tableStyle==null)
{
tableStyle = new DataGridTableStyle();
tableStyle.MappingName =dvw.Table.TableName;
int numCols = dvw.Table.Columns.Count;
TextColumnStyle textColumn;
for (int i=0;i<dvw.Table.Columns.Count;i++)
{
textColumn = new TextColumnStyle();
textColumn.ReadOnly=false;
textColumn.NullText="";
textColumn.HeaderText = dvw.Table.Columns[i].Caption;
textColumn.MappingName = dvw.Table.Columns[i].ColumnName;
tableStyle.GridColumnStyles.Add(textColumn);
} tableStyle.AllowSorting=false;
tableStyle.RowHeadersVisible=false;
this.dataGrid1.TableStyles.Clear();
this.dataGrid1.TableStyles.Add(tableStyle);
}
this.dataGrid1.BeginInit();
this.dataGrid1.SetDataBinding(dvw,null);
this.dataGrid1.EndInit();
dvw.AllowDelete=true;
dvw.AllowEdit=true;
}
private void button1_Click(object sender, System.EventArgs e)
{
string strConn="server=(local);database=mydata;uid=sa;pwd=sa";
SqlConnection cn=new SqlConnection(strConn);
SqlDataAdapter ada=new SqlDataAdapter();
ada.SelectCommand=new SqlCommand("select * from system_personnel",cn);
DataSet ds=new DataSet();
ada.Fill(ds);
this.SetListDataEX(ds.Tables[0].DefaultView);
}