DataGrid skills (four)

zhaozj2021-02-16  43

DataGrid skills (four)

------------ How to display data from different DataTable in DataGrid

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 of the frequently encountered problems is: How to display the field combination from different DATATABLE in DataGrid? Of course, data can be merged into a DataTable, but sometimes due to other restrictions cannot be merged, the following provides a method of displaying data from different DataTable (in a DataSet) in the DataGrid.

Data in DataSet

We hope that the data is displayed in DataGrid as shown below.

Here are an example of how to implement how to display data from different DataTable in the DataGrid.

Basic ideas: Obviously, DataGrid can't bind two DataTable at the same time, then make it display two DataTable data, only in DataGridColumn. Take the above structure as an example, we build a class DataGridJointboxColumn inherited DataGridTextBoxColumn, overloading the getColumnValueatrow method. The functionality to be implemented is: Data Source is DataTable1, the return value is DataTable2.lastname or DataTable2.Firstname corresponding to DataTable1.employeeid. DataTable1.employeeID is 1-to-many contacts, and we can add this contact to the DataSet, you can get the value of DataTable1.lastName and DataTable2.FirstName corresponding to DataTable1.Employeeid by getParenTrow method. The specific code is as follows:

public class JoinTextBoxColumn: DataGridTextBoxColumn {string relation; // 1 represents DataTable1.EmployeeID DataTable2.EmployeeID with multi-contact DataColumn col; // return DataTable2 column indicates public JoinTextBoxColumn (string relation, DataColumn col) {this.relation = relation; this.col = col; base.ReadOnly = true;} protected override object GetColumnValueAtRow (CurrencyManager cm, int RowNum) {try {DataRow dr = ((DataView) cm.List) [RowNum] .Row; DataRow parentdr = dr.GetParentRow (RELATION); RETURN PARENTDR [COL]; // Return value is the lastname or firstname} catch {return "" corresponding to DATATABLE1.Employeeid, ""; // Avoid eventual (CurrencyManager CM) when adding new rows}} protected override bool INT rownum) {return false;} public new bo}}}} The following code shows how to use class DataGridjoin COLUMN. Create a Windows program, join a DataGrid and a SQLCONNECTION, connect the database Northwind. Add the following code in Form_Load:

private void Form1_Load (object sender, System.EventArgs e) {SqlDataAdapter sda1 = new SqlDataAdapter ( "select EmployeeID, LastName, FirstName from Employees", this.sqlConn); SqlDataAdapter sda3 = new SqlDataAdapter ( "select OrderID, EmployeeID, OrderDate, RequiredDate From Orders ", this.sqlconn;

DS = new dataset (); SDA1.FILL (DS, "EMP"); SDA3.FILL (DS, "ORD");

DS.RELATIONS.ADD ("ORD_EMP", DS.TABLES ["EMP"]. Column ["EmployeeID"], DS.Tables ["ORD"]. Column ["EmployeeID"]); ds.tables ["ORD" ] .Columns.Add ( "lastName", typeof (string));. ds.Tables [ "ord"] Columns.Add ( "firstName", typeof (string)); DataGridTableStyle dt = new DataGridTableStyle (); DataGridColumnStyle dc; dc = new DataGridTextBoxColumn (); dc.MappingName = "OrderID"; dc.HeaderText = "OrderID"; dt.GridColumnStyles.Add (dc); dc = new DataGridTextBoxColumn (); dc.MappingName = "EmployeeID"; dc.HeaderText = "EmployeeID"; DT.GridColumnStyles.Add (DC);

dc = new JoinTextBoxColumn ( "ord_emp", ds.Tables [ "emp"] Columns [ "firstName"].); dc.MappingName = "firstName"; dc.HeaderText = "firstName"; dt.GridColumnStyles.Add (dc) ;

DC = New JointextBoxColumn ("ORD_EMP", DS.Tables ["EMP"]. Column ["Lastname"]); dc.mappingname = "lastname"; dc.headertext = "LastName"; DT.GridColumnStyles.Add (DC) ;

DC = New DataGridTextBoxColumn (); dc.mappingname = "ORDERDATE"; DC.Headertext = "ORDERDATE"; DT.GRIDCOLUMNSTYLES.ADD (DC);

DC = New DataGridTextBoxColumn (); dc.mappingname = "requireddate"; dc.headertext = "RequiredDate"; DT.GridColumnStyles.Add (DC);

Dt.mappingName = "ORD"; this.DataGrid1.tableStyles.Add (DT);

This.DataGrid1.datasource = DS; this.DataGrid1.datamember = "ORD";

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

New Post(0)