DataGrid Tips (2)

zhaozj2021-02-16  43

DataGrid Tips (2)

------------ How to implement multi-line head

Sometimes I listen to some friends complaining that .NET's DataGrid is not very easy. As far as my personal experience, DataGrid's function is very powerful, which can make us complete a variety of work, but unfortunately, it is not simple enough. I have accumulated some solutions to some problems that I often encountered, and now I summarize them for your reference. One problem that is often encountered is: We hope that the header of DataGrid is multi-line (Figure 1). I have been looking for a solution for a long time I haven't found a solution, and later I thought of the CAPTIONTEXT and CAPTIONFONT attributes of DataGrid. So I would like to draw multiple lines of heads in the display area of ​​CAPTION. The following sample code implements this idea, as shown in Figure 1.

First, you need to write a class to represent the sheet of self-drawn, this class records the display text, icon, and the information belonging to the columns it juropteed.

// Header class public class TopHeaderColumn {public TopHeaderColumn () {this.columnCollection = new ArrayList ();} private string caption; // text header public string Caption {get {return caption;} set {caption = value ;}} private ArrayList columnCollection; // record information pertaining to the columns of the header jurisdiction (the collection by adding to the object) public ArrayList ColumnCollection {get {return this.columnCollection;} set {this.columnCollection = value;} } Private int width; // Width of the header PUBLIC INT WIDTH {GET {Return Width;} set {width = value;}} private image image = null; // Metet Icon PUBLIC Image Image;} set {image = value;}}

} In addition, because the next code requires the location of the DataGrid horizontal scroll bar, and the DataGrid cannot obtain the position of the horizontal scroll bar, so you need to make a little modification to the DataGrid. Public Class MyDataGrid: DataGrid {public scrollbar hscrollbar {get {return this.hizscrollbar;}}} Ok, you can work. Create a new Window application, join a MyDataGrid, SqlConnection, and ImageList, connect the SQL database Northwind. Of course, we have added the above code namespace WindowsApplication1 {public class Form1: System.Windows.Forms.Form {private System.Data.SqlClient.SqlConnection sqlConnection1; private myDataGrid dataGrid1; private ArrayList al; private System.Windows.Forms.ImageList imageList1 // Join this sentence in InitializeComponent (): this.dataGrid1 = new mydataGrid (), and sets other DataGrid properties according to your needs. Note that CaptionVisible must be set to true, captionText = "" ". private void Form1_Load (object sender, System.EventArgs e) {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";

// columns in the 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); // The width change adjusts the header 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);

DTS.MAppingName = "EMPLOYEES"; this.dataGrid1.tablestyles.add (dts);

// Establish a self-drawn head class and add them to the collection al al = new arraylist (); topheaderColumn tc1 = new TopHeaderColumn (); tc1.caption = "name"; tc1.image = this.imagelist1.images [0]; Tc1.columncollection.Add (0); // Record the index tc1.columncollection.add (1) of the column it jurisdiction; tc1.width = c1.width c2.width; TopHeaderColumn TC2 = New TopHeaderColumn (); tc2.caption = "Address"; tc2.columncollection.Add (2); tc2.columncollection.Add (3); tc2.width = c3.width c4.width; al. ADD (TC1); Al.Add (TC2); this .DataGrid1.paint = new system.windows.Forms.PainteventHandler (this.DataGrid1_paint);}

Private void DataGrid1_paint (Object Sender, System.Windows.Forms. Painteventargs E) {INT x = 0; // Calculate the left distance of the first header INT LEFT = this.dataGrid1.tablestyles [0]. RowHeaderwidth-this. DataGrid1.hscrollBar.Value; // Traversing the head collection Al, draws the header Foreach (Object O in this.al) {// calculates the left distance of the header (text center) x = left ((TopHeaderColumn) o) .Width-convert.Toint32 (E.Graphics. MeasureString ((TopHeaderColumn) o) .caption, this.dataGrid1.captionFont). Width)) / 2; // Complete the header Draw IF ((TopHeaderColumn) O ) .Image! = Null) E.Graphics.drawImage ((TopHeaderColumn) o) .image, x-imagelist1.images [0] .size.width, 0); E.Graphics.drawstring ((TopHeaderColumn) O) .Caption, this.dataGrid1. CaptionFont, New Solidbrush (THISDATAGRID1.CAPTIONFORECOLOR), X, 0); IF (x> 0) E.Graphics.drawline (New Pen (Color.Black, 2), Left-1, 0, Left-1, this.DataGrid1.Height; // Calculate the left distance of the next header Left = ((TopHeaderColumn) o) .width;} if (x

Private Void ABC (Object Sender, Eventargs E) {// Sets the width of the header foreach (Object O in this.al) {(TopHeaderColumn) o) .width = 0; Foreach (INT I in (TopHeaderColumn) O) .ColumnCollection) {((TopHeaderColumn) o) .Width = this.dataGrid1.TableStyles [0] .GridColumnStyles [i] .Width;}}} private void dataGrid1_Scroll (object sender, System.EventArgs e) {this.dataGrid1.Refresh ();

The above code implements two-layer header, as for the three-layer header.

About how to implement a DataGrid multi-layer header, many netizens mentioned that there is no particularly convenient way. If the netizen discovers a better way, please leave a short train to inform me, thank you.

转载请注明原文地址:https://www.9cbs.com/read-26529.html

New Post(0)