Description: This example is done in the Factory row machine report
First, the GridView column is initialized according to the date of the query
private void Inigridview (DateTime p_dates,datetime p_datee) {//1. Remove the column outside the machine for (int i = grid view1.columns.count-1; I >= 0; i--)//Remove from the back-forward traversal, otherwise it will cause confusion. No foreach is also for this reason {if (gridview1.columns[i]. FieldName! = "Machineno")//machineno: Machine {gridView1.Columns.RemoveAt (i);//Removal }}//2. Add column//2-1 based on date. Interval length TimeSpan TS1 = new TimeSpan (sysconvert.todate Time (p_dates). Ticks);//sysconvert is the framework I used to customize, can be replaced by the convert TimeSpan TS2 = new TimeSpan (Sysconvert.todatetime (p_datee). Ticks); TimeSpan TS = TS1. Subtract (TS2). Duration (); int days = TS. Days; if (days>100)//limit do not exceed 100 days, prevent the user mistakenly selected too long date, can be adjusted according to the actual situation {return; }//2-2. Loop increment column for (int i = 0; I <= days; i++) {//define column name as Date DateTime coldate = p_dates.adddays (i); Add columns DevExpress.XtraGrid.Columns.GridColumn column = new DevExpress.XtraGrid.Columns.Grid Column (); Column. FieldName = Coldate. ToString ("Yyyy-mm-dd");//The date is used as the field name, where special attention needs to be taken, when populating the data, the Attribution column column is determined by date. Caption = Coldate. ToString ("m.dd");//Display Name column. Visible = true;//Visibility column. Visibleindex = i;//ordinal must be set, otherwise the default-1, still not visible column. Columnedit = This.txtmemo; GRIDVIEW1.COLUMNS.ADD (column); } }
Ii. Handling of the DataTable
Public Override voidBindgrid () {stringsql ="SELECT Machineno from Data_machinemanage WHERE 1=1";//Data_machinemanage is a data sheet that stores all the machinesDataTable dt = Sysutils.fill (SQL);//Sysutils as framework-specific methods, non-universal methods, depending on the actual situation replacedProc (dt,txtformdates.datetime,txtformdatee.datetime); GridView1.GridControl.DataSource=DT; GridView1.GridControl.Show (); }/// <summary>///processing data tables, filling the machine information/// </summary> /// <param name= "DT" ></param> Private voidProc (DataTable p_dt,datetime p_dates,datetime p_datee) {//the number of columns in the Extended DT table is consistent with the GridView column numberTimeSpan TS1 =NewTimeSpan (Sysconvert.todatetime (p_dates). Ticks); TimeSpan TS2=NewTimeSpan (Sysconvert.todatetime (p_datee). Ticks); TimeSpan TS=TS1. Subtract (TS2). Duration (); intDays =TS. Days; if(days> -) { return; } for(inti =0; I <= days; i++) {DateTime coldate= P_dates.adddays (i);//take the date of the day as the column name, consistent with the GridViewP_dt. Columns.Add (coldate. ToString ("YYYY-MM-DD"),typeof(string));//Adding Columns } //fill in the grid according to the data stringsql ="SELECT * from Uv1_sale_soreviewplan WHERE prodate between '"+p_dates.tostring () +"' and '"+p_datee.tostring () +"'";//Note: The query out of a record contains the following information: Date prodate, order Orderformno, Machine Machineno. A record contains only one day of machine scheduling information, 10.1-10.5th is 6 records, not aDataTable DT= Sysutils.fill (SQL);//Take out all the machine records that meet the criteria foreach(DataRow Drinchdt. Rows) {//Defining display Strings stringstr=dr["Orderformno"]. ToString (); //define the machine for easy query stringJT = dr["Machineno"]. ToString (); //the date of the current row to determine which column belongs to the P_DT table stringProdate = Sysconvert.todatetime (dr["prodate"]). ToString ("YYYY-MM-DD"); Datarow[] P_drarr= P_dt. Select ("machineno="+ sysstring.todbstring (JT));//Take out the machine in the machine table . if(P_drarr. length==0)//This cycle is interrupted without this machine { Continue; } Else{DataRow P_dr= p_drarr[0];//Remove the first line that meets the criteriaP_dr[prodate] = str;//here prodate is the column name, and the field for each date in the GridView and P_dt is a certain day of the year } } }
Attached:
Show Gantt (Gantt Chart) with GridView, add or subtract columns dynamically