Create a DataGrid data column manually (C#window)

Source: Internet
Author: User
Tags table name
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");




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.