datagrid|window| Create | data
I used to rarely use vs.net in the DataGrid, because I think it is not very good, yesterday, suddenly remembered, with a simple class, to achieve the manual creation of data columns, so you can customize the column name and bound data fields, use up comparison of the ~ ~
Class: ExDataGrid.cs
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Namespace WindowsApplication5
{
<summary>
Summary description of the Exdatagrid.
</summary>
public class Exdatagrid
{
Private System.Windows.Forms.DataGrid DataGrid;
private string Strmapname;
private static DataGridTableStyle dgt;
Strmapname: Table name;
Public Exdatagrid (System.Windows.Forms.DataGrid datagrid,string strmapname)
{
This.datagrid=datagrid;
This.strmapname=strmapname;
DataGrid.TableStyles.Clear ();
DataGridTableStyle Tblstyle = new DataGridTableStyle ();
Tblstyle.mappingname = This.strmapname;
DATAGRID.TABLESTYLES.ADD (Tblstyle);
Dgt=tblstyle;
}
Parameters:
1--i:gridcolumnstyles;
2--strmap:mappingname;
3--strheader:headertext
public static void Createcols (int i,string strmap,string strheader)
{
Createcols (i,strmap,strheader,80);
}
Parameters:
Intwidth: Data Column column width
public static void Createcols (int i,string strmap,string strheader,int intwidth)
{
You can set the style of the column here
DataGridColumnStyle Textcol = new DataGridTextBoxColumn ();
DataGridTableStyle Tblstyle=dgt;
TBLSTYLE.GRIDCOLUMNSTYLES.ADD (Textcol);
Tblstyle.gridcolumnstyles[i]. MappingName = Strmap;
Tblstyle.gridcolumnstyles[i]. HeaderText = Strheader;
Tblstyle.gridcolumnstyles[i]. Width = intwidth;
Tblstyle.gridcolumnstyles[i]. Nulltext = string. Empty;
}
}
}
Call:
This.dataGrid1.SetDataBinding (DataSet, "TableName");
Exdatagrid edg =new Exdatagrid (THIS.DATAGRID1, "TableName");
Exdatagrid.createcols (0, "ProductCode", "Product number");
Exdatagrid.createcols (1, "ProductName", "Product Name");
Exdatagrid.createcols (2, "Productclass", "Product class");