Define DataGridView Column type Programmatically

后端 未结 6 1792
眼角桃花
眼角桃花 2020-12-10 08:06

I need a way in which I can define the column type at run-time.

Here is my code:

foreach (DataGridViewColumn column in this.dataGrid.Columns)
{
???
/         


        
相关标签:
6条回答
  • 2020-12-10 08:40

    I'm not 100% certain I understand you question, but you can specify the column type when you create the column:

    foreach (var definition in columnDefinitions)  // Some list of what the column types are
    {
        var columnSpec = new DataColumn
            {
                DataType = definition.Type, // This is of type System.Type
                ColumnName = defintion.Name // This is of type string
            };
    
        this.dataGrid.Columns.Add(columnSpec);
    }
    

    If you need to change the type once it's been created - you can't do that. The best you can do is delete the columns and recreate them with the new types.

    0 讨论(0)
  • 2020-12-10 08:41

    Assuming your'e using BindingSource

    var cbox = new DataGridViewCheckBoxColumn // Modify column type
    {
        AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells,
        DataPropertyName = dgv.Columns["ColumnWantToChange"].Name, 
        HeaderText = "SOME HEADER NAME"
    };
    dgv.Columns.Add(cbox); // Add new 
    var r = dgv.Columns.OfType<DataGridViewTextBoxColumn>().Where(x => x.Name == "ColumnWantToChange").FirstOrDefault();
    dgv.Columns.Remove(r); // Remove the original column
    
    0 讨论(0)
  • 2020-12-10 08:45

    you can assign button column also and you can assign properties also like this

        DataGridViewButtonColumn column = new DataGridViewButtonColumn();
        datagridview1.Columns.Add(column);
        column.FlatStyle = FlatStyle.System;
        column.DefaultCellStyle.ForeColor = Color.ForestGreen;          
    
    0 讨论(0)
  • 2020-12-10 08:46

    I assume you mean when you create the DataGridView. In that case you can define it in a DataTable and hook it up to the dgv's Datasource:

    For example:

    var columns = new List<Tuple<string, string>>();
    columns.Add(new Tuple<string, string>("Name", "System.String"));
    columns.Add(new Tuple<string, string>("Selected", "System.Boolean"));
    columns.Add(new Tuple<string, string>("Id", "System.Int32"));
    var table = new DataTable();
    columns.ForEach(c => table.Columns.Add(new DataColumn(c.Item1) { DataType = Type.GetType(c.Item2) }));
    var dgv = new DataGridView();
    dgv.DataSource = table;
    
    0 讨论(0)
  • 2020-12-10 08:50

    You can't change the type of a DataGridView column after it is created but there is nothing to stop you creating columns as needed at run-time.

    So, depending on the logic the determines the type of each column, you create columns as needed and add them to the DataGridView.

    An example of creating a checkbox column is below:

    DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn()
    dataGridView1.Columns.Add(col);
    

    Without any more information on what determines your column types it is hard to give more advice, but you could easily use this technique with a DataTable, inspecting the type of each of its columns, or even using reflection over an object you are binding the datagridview to.

    0 讨论(0)
  • If we consider your example;

    foreach (DataGridViewColumn column in this.dataGrid.Columns)
    {
        column.ValueType = typeof(DateTime);
    }
    

    But, assuming you do not want all the columns in your datagridview to be of the same type;

    this.datagrid.Columns[0].ValueType = typeof(Int32);
    this.datagrid.Columns[1].ValueType = typeof(String);
    this.datagrid.Columns[2].ValueType = typeof(DateTime);
    

    This is especially useful when you are using a datasource and not adding your own columns programatically.

    0 讨论(0)
提交回复
热议问题