How to set up signature for DataGridView

I am using DataGridView in WinForm application to display data table. Everything works fine except for the Caption DataColumn property. I tried to set the Caption property, but it seems that the DataGridView is using the DataColumn name as the title instead of the Caption property value?

Have a google for this and it looks like this caption property is intentionally disabled.

My WinForm app is localized and I need to show Chinese signature. Does anyone know how I can do this?

Here is my code for setting up data table

// Create a new DataTable.
DataTable table = new DataTable("Payments");

// Declare variables for DataColumn and DataRow objects.
DataColumn column;
DataRow row;

// Create new DataColumn, set DataType, 
// ColumnName and add to DataTable.    
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id";
column.ReadOnly = true;
column.Unique = true;
column.Caption = LocalizedCaption.get("id") //LocalizedCaption is my library to retrieve the chinese caption

// Add the Column to the DataColumnCollection.
table.Columns.Add(column);


// Create three new DataRow objects and add them to the DataTable
for (int i = 0; i <= 2; i++)
{
    row = table.NewRow();
    row["id"] = i;
    table.Rows.Add(row);
}

//assign the DataTable as the datasource for a DataGridView
dataGridView1.DataSource = table;

      

+1
c # internationalization winforms datagridview


a source to share


2 answers


This worked for me:



private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    var dGrid = (sender as DataGrid);
    if (dGrid == null) return ;
    var view = dGrid.ItemsSource as DataView;
    if (view == null) return;
    var table = view.Table;
    e.Column.Header = table.Columns[e.Column.Header as String].Caption;
}

      

+1


a source to share


You have several options. Here's a quick fix that should work, just add this to the end of your code block:

        //Copy column captions into DataGridView
        for (int i = 0; i < table.Columns.Count; i++) {
            if (dataGridView1.Columns.Count >= i) {
                dataGridView1.Columns[i].HeaderText = table.Columns[i].Caption;
            }
        }

      



As you can see, this just copies your existing column headers to the correct HeaderText property of each DataGridView column. This assumes that no previous columns exist in the DataGridView before you bind the DataTable.

+3


a source to share







All Articles