C#中DataGrid使用技巧
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
SqlDataAdapter da=new SqlDataAdapter("select lastname, firstname, Address,City from employees",this.sqlConnection1);
DataSet ds=new DataSet(); da.Fill(ds,"employees"); this.dataGrid1.DataSource=ds; this.dataGrid1.DataMember="employees";
首先需要编写一个类来表示自画的表头,这个类将记录表头的显示文本、图标和属于它管 辖的列的信息。
//表头类 public class TopHeaderColumn {
public TopHeaderColumn() { this.columnCollection=new ArrayList(); } private string caption; //表头的显示文本 public string Caption {
private int width; //表头的宽度 public int Width { get {return width;} set {width=value;} } private Image image=null; //表头的图标 public Image Image {
get {return image;} set {image=value;} }
al.Add(tc1); al.Add(tc2); this.dataGrid1.Paint += new System.Windows.Forms.PaintEventHandler(this.dataGrid1_Paint); }
private myDataGrid dataGrid1; private System.Data.SqlClient.SqlConnection sqlConnection1; //加入全局变量 oldValue,用它表示单元格原来的文本。 private string oldValue;
private void Form1_Load(object sender, System.EventArgs e) { oldValue=""; SqlDataAdapter sda=new SqlDataAdapter("select LastName,FirstName from employees",this.sqlConnection1);
} }
好了,可以工作了。新建一个 Window 应用程序,加入一个 myDataGrid、SqlConnection 和 ImageList,连接 SQL 数据库 NorthWind。当然,还得加入上面的代码
namespace WindowsApplication1 {
public class Form1 : System.Windows.Forms.Form {
比较经常碰到的一个问题是:我们希望 DataGrid 的某一列只能输入特定的文本,比如: 不能输入数字。下面的例子说明如何实现这种功能。
新建一个 Window 应用程序,加入一个 DataGrid 和 SqlConnection,连接 SQL 数据库 NorthWind。
namespace WindowsApplication1 { public class Form1 : System.Windows.Forms.Form {
宽度
//设置 DataGrid 的各列 DataGridTextBoxColumn c1=new DataGridTextBoxColumn(); DataGridTextBoxColumn c2=new DataGridTextBoxColumn(); DataGridTextBoxColumn c3=new DataGridTextBoxColumn(); DataGridTextBoxColumn c4=new DataGridTextBoxColumn(); c1.MappingName="lastname"; c2.MappingName="firstname"; c3.MappingName="Address"; c4.MappingName="City"; c1.HeaderText="lastname"; c2.HeaderText="firstname"; c3.HeaderText="Address"; c4.HeaderText="City"; c1.WidthChanged+=new EventHandler(this.abc);//列的宽变动时调整表头
Add(dts);
dts.MappingName="employees"; this.dataGrid1.TableStyles.
//建立自画的表头类并将它们加入集合 al al=new ArrayList(); TopHeaderColumn tc1=new TopHeaderColumn(); tc1.Caption="Name"; tc1.Image=this.imageList1.Images[0]; tc1.ColumnCollection.Add(0);//记录它管辖的列的 index tc1.ColumnCollection.Add(1); tc1.Width=c1.Width+c2.Width;
就我个人的体会datagrid的功能非常强大可以使我们随心所欲的完成各种各样的工作可惜就是实现起来不够简单明了
C#中 DataGrid 使用技巧 (一)
有时候听有些朋友抱怨.NET 的 DataGrid 不是很好用。就我个人的体会,DataGrid 的功能非 常强大,可以使我们随心所欲的完成各种各样的工作,可惜就是实现起来不够简单明了。我对平 时经常碰到的一些问题积累了一些解决的方法,现在把它们总结一下供大家参考。
} 另外,因为以后的代码需要 DataGrid 水平滚动条的位置,而 DataGrid 无法取得水平滚 动条的位置,所以需要对 DataGrid 做一点修改。
public class myDataGrid:DataGrid {
public ScrollBar HScrollBar {
get {return this.HorizScrollBar;}
//当某一单元格获得焦点时,记录单元格的文本 oldValue=((DataGridTextBoxColumn) this.dataGrid1.TableStyles[0].GridColumnStyles[1]).TextBox.Text; } private void DataGridTextChanged(object sender,EventArgs e) {
private System.Data.SqlClient.SqlConnection sqlConnection1; private myDataGrid dataGrid1; private ArrayList al; private System.Windows.Forms.ImageList imageList1; \\在 InitializeComponent()里加入这一句:this.dataGrid1 = new myDataGrid(),并根据你的需要设置其他的 DataGrid 属性。注意,CaptionVisible 必须设 为 true,CaptionText=""。 private void Form1_Load(object sender, System.EventArgs e) {
get {return caption;} set {caption=value;} } private ArrayList columnCollection; //用来记录属于表头管辖的各列的信息(通过往集合里添加 object) public ArrayList ColumnCollection { get {return this.columnCollection;} set {this.columnCollection=value;} }
int index=0; string str=((DataGridTextBoxColumn)this.dataGrid1.TableStyles[0].GridColumnStyles[1 ]).TextBox.Text; //当单元格的文本改变时,检验是否有非法字符 while(index<str.Length) { //如果发现数字,显示错误信息并将单元格还原为原内容 if (Char.IsDigit(str,index)) { MessageBox.Show("不能输入数字,请重新输入"); ((DataGridTextBoxColumn)this.dataGrid1.TableStyles[0].GridColumnStyl es[1]).TextBox.Text=oldValue; return; }
DataSet ds=new DataSet(); sda.Fill(ds,"employees"); DataGridTableStyle ats=new DataGridTableStyle(); ats.MappingName="employees"; DataGridColorColumn dcs1=new DataGridColorColumn(); dcs1.HeaderText="lastname"; ats.GridColumnStyles.Add(dcs1); DataGridTextBoxColumn dcs2=new DataGridTextBoxColumn(); dcs2.HeaderText="firstname"; dcs2.MappingName="FirstName"; dcs2.TextBox.TextChanged+=new EventHandler(DataGridTextChanged); dcs2.TextBox.Enter+=new EventHandler(DataGridTextBox_Enter); ats.GridColumnStyles.Add(dcs2); this.dataGrid1.TableStyles.Add(ats); this.dataGrid1.DataSource=ds; this.dataGrid1.DataMember="employees"; } private void DataGridTextBox_Enter(object sender,EventArgs e) {
TopHeaderColumn tc2=new TopHeaderColumn(); tc2.Caption="Address"; tc2.ColumnCollection.Add(2); tc2.ColumnCollection.Add(3); tc2.Width=c3.Width+c4.Width;
c2.WidthChanged+=new EventHandler(this.abc); c3.WidthChanged+=new EventHandler(this.abc); c4.WidthChanged+=new EventHandler(this.abc);
DataGridTableStyle dts=new DataGridTableStyle(); dts.GridColumnStyles.Add(c1); dts.GridColumnStyles.Add(c2); dts.GridColumnStyles.Add(c3); dts.GridColumnStyles.Add(c4);
index++; } } }
(二)
我们希望 DataGrid 的表头是多行的(图 1)。我在网上找了很久也找不到解决的方法,后 来想到了 DataGrid 的 CaptionText 和 CaptionFont 属性。于是我就想能不能在 Caption 的 显示区域画出多行表头。下面的示例代码实现了这个想法。
DataSet ds=new DataSet(); da.Fill(ds,"employees"); this.dataGrid1.DataSource=ds; this.dataGrid1.DataMember="employees";
首先需要编写一个类来表示自画的表头,这个类将记录表头的显示文本、图标和属于它管 辖的列的信息。
//表头类 public class TopHeaderColumn {
public TopHeaderColumn() { this.columnCollection=new ArrayList(); } private string caption; //表头的显示文本 public string Caption {
private int width; //表头的宽度 public int Width { get {return width;} set {width=value;} } private Image image=null; //表头的图标 public Image Image {
get {return image;} set {image=value;} }
al.Add(tc1); al.Add(tc2); this.dataGrid1.Paint += new System.Windows.Forms.PaintEventHandler(this.dataGrid1_Paint); }
private myDataGrid dataGrid1; private System.Data.SqlClient.SqlConnection sqlConnection1; //加入全局变量 oldValue,用它表示单元格原来的文本。 private string oldValue;
private void Form1_Load(object sender, System.EventArgs e) { oldValue=""; SqlDataAdapter sda=new SqlDataAdapter("select LastName,FirstName from employees",this.sqlConnection1);
} }
好了,可以工作了。新建一个 Window 应用程序,加入一个 myDataGrid、SqlConnection 和 ImageList,连接 SQL 数据库 NorthWind。当然,还得加入上面的代码
namespace WindowsApplication1 {
public class Form1 : System.Windows.Forms.Form {
比较经常碰到的一个问题是:我们希望 DataGrid 的某一列只能输入特定的文本,比如: 不能输入数字。下面的例子说明如何实现这种功能。
新建一个 Window 应用程序,加入一个 DataGrid 和 SqlConnection,连接 SQL 数据库 NorthWind。
namespace WindowsApplication1 { public class Form1 : System.Windows.Forms.Form {
宽度
//设置 DataGrid 的各列 DataGridTextBoxColumn c1=new DataGridTextBoxColumn(); DataGridTextBoxColumn c2=new DataGridTextBoxColumn(); DataGridTextBoxColumn c3=new DataGridTextBoxColumn(); DataGridTextBoxColumn c4=new DataGridTextBoxColumn(); c1.MappingName="lastname"; c2.MappingName="firstname"; c3.MappingName="Address"; c4.MappingName="City"; c1.HeaderText="lastname"; c2.HeaderText="firstname"; c3.HeaderText="Address"; c4.HeaderText="City"; c1.WidthChanged+=new EventHandler(this.abc);//列的宽变动时调整表头
Add(dts);
dts.MappingName="employees"; this.dataGrid1.TableStyles.
//建立自画的表头类并将它们加入集合 al al=new ArrayList(); TopHeaderColumn tc1=new TopHeaderColumn(); tc1.Caption="Name"; tc1.Image=this.imageList1.Images[0]; tc1.ColumnCollection.Add(0);//记录它管辖的列的 index tc1.ColumnCollection.Add(1); tc1.Width=c1.Width+c2.Width;
就我个人的体会datagrid的功能非常强大可以使我们随心所欲的完成各种各样的工作可惜就是实现起来不够简单明了
C#中 DataGrid 使用技巧 (一)
有时候听有些朋友抱怨.NET 的 DataGrid 不是很好用。就我个人的体会,DataGrid 的功能非 常强大,可以使我们随心所欲的完成各种各样的工作,可惜就是实现起来不够简单明了。我对平 时经常碰到的一些问题积累了一些解决的方法,现在把它们总结一下供大家参考。
} 另外,因为以后的代码需要 DataGrid 水平滚动条的位置,而 DataGrid 无法取得水平滚 动条的位置,所以需要对 DataGrid 做一点修改。
public class myDataGrid:DataGrid {
public ScrollBar HScrollBar {
get {return this.HorizScrollBar;}
//当某一单元格获得焦点时,记录单元格的文本 oldValue=((DataGridTextBoxColumn) this.dataGrid1.TableStyles[0].GridColumnStyles[1]).TextBox.Text; } private void DataGridTextChanged(object sender,EventArgs e) {
private System.Data.SqlClient.SqlConnection sqlConnection1; private myDataGrid dataGrid1; private ArrayList al; private System.Windows.Forms.ImageList imageList1; \\在 InitializeComponent()里加入这一句:this.dataGrid1 = new myDataGrid(),并根据你的需要设置其他的 DataGrid 属性。注意,CaptionVisible 必须设 为 true,CaptionText=""。 private void Form1_Load(object sender, System.EventArgs e) {
get {return caption;} set {caption=value;} } private ArrayList columnCollection; //用来记录属于表头管辖的各列的信息(通过往集合里添加 object) public ArrayList ColumnCollection { get {return this.columnCollection;} set {this.columnCollection=value;} }
int index=0; string str=((DataGridTextBoxColumn)this.dataGrid1.TableStyles[0].GridColumnStyles[1 ]).TextBox.Text; //当单元格的文本改变时,检验是否有非法字符 while(index<str.Length) { //如果发现数字,显示错误信息并将单元格还原为原内容 if (Char.IsDigit(str,index)) { MessageBox.Show("不能输入数字,请重新输入"); ((DataGridTextBoxColumn)this.dataGrid1.TableStyles[0].GridColumnStyl es[1]).TextBox.Text=oldValue; return; }
DataSet ds=new DataSet(); sda.Fill(ds,"employees"); DataGridTableStyle ats=new DataGridTableStyle(); ats.MappingName="employees"; DataGridColorColumn dcs1=new DataGridColorColumn(); dcs1.HeaderText="lastname"; ats.GridColumnStyles.Add(dcs1); DataGridTextBoxColumn dcs2=new DataGridTextBoxColumn(); dcs2.HeaderText="firstname"; dcs2.MappingName="FirstName"; dcs2.TextBox.TextChanged+=new EventHandler(DataGridTextChanged); dcs2.TextBox.Enter+=new EventHandler(DataGridTextBox_Enter); ats.GridColumnStyles.Add(dcs2); this.dataGrid1.TableStyles.Add(ats); this.dataGrid1.DataSource=ds; this.dataGrid1.DataMember="employees"; } private void DataGridTextBox_Enter(object sender,EventArgs e) {
TopHeaderColumn tc2=new TopHeaderColumn(); tc2.Caption="Address"; tc2.ColumnCollection.Add(2); tc2.ColumnCollection.Add(3); tc2.Width=c3.Width+c4.Width;
c2.WidthChanged+=new EventHandler(this.abc); c3.WidthChanged+=new EventHandler(this.abc); c4.WidthChanged+=new EventHandler(this.abc);
DataGridTableStyle dts=new DataGridTableStyle(); dts.GridColumnStyles.Add(c1); dts.GridColumnStyles.Add(c2); dts.GridColumnStyles.Add(c3); dts.GridColumnStyles.Add(c4);
index++; } } }
(二)
我们希望 DataGrid 的表头是多行的(图 1)。我在网上找了很久也找不到解决的方法,后 来想到了 DataGrid 的 CaptionText 和 CaptionFont 属性。于是我就想能不能在 Caption 的 显示区域画出多行表头。下面的示例代码实现了这个想法。