Marcelo Camargo
Marcelo Camargo

Reputation: 2298

Add TableStyles to DataGridView

When I was developing for Windows CE, I had a component called DataGrid. Now, yet using Windows Forms, in a most recent version, I have just DataGridView and it doesn't have TableStyles. I'm trying to manipulate the DataGridTextBoxColumns of it, but I'm unable to do it. I'm binding for it data from a list of KeyValuePair and I don't know how I can control somethings of its columns, like MappingName, HeaderText or Width. My code is currently the following:

public IList<KeyValuePair<int, string>> Lista
{
    set
    {
        dgwWorkitem.DataSource = value;
        DataGridTableStyle tabela = new DataGridTableStyle
        {
            MappingName = dgwWorkitem.GetType().Name
        };
        DataGridTextBoxColumn id = new DataGridTextBoxColumn
        {
            MappingName = @"Key",
            HeaderText = @"Id",
            Width = (((dgwWorkitem.Width) / 2) / 2)
        };
        DataGridTextBoxColumn titulo = new DataGridTextBoxColumn
        {
            MappingName = @"Value",
            HeaderText = @"Título",
            Width = (((dgwWorkitem.Width) / 2) + ((dgwWorkitem.Width / 2) / 2))
        };
        tabela.GridColumnStyles.Add(id);
        tabela.GridColumnStyles.Add(titulo);
        // dgwWorkitem.TableStyles.Clear();
        // dgwWorkitem.TableStyles.Add(tabela);
    }
}

I use a set to bind the data and after I want to manipulate, but TableStyles from DataGrid are not accepted in DataGridView. How do I manipulate them without creating a DataGridTableStyle?

Upvotes: 3

Views: 3679

Answers (1)

Marcelo Camargo
Marcelo Camargo

Reputation: 2298

You just don't need to create a new DataGridTableStyle. Knowing its indexes, just manipulate each property individually:

public IList<KeyValuePair<int, string>> Lista
{
    set
    {
        dgwWorkitem.DataSource = value;
        dgwWorkitem.Columns[0].HeaderText = @"Id";
        dgwWorkitem.Columns[0].Width = (((dgwWorkitem.Width) / 2) / 2);
        dgwWorkitem.Columns[1].HeaderText = @"Título";
        dgwWorkitem.Columns[1].Width = (((dgwWorkitem.Width) / 2) + ((dgwWorkitem.Width / 2) / 2));
    }
}

This way you can handle the Id and the Título like you would like to do, same as before.

Upvotes: 3

Related Questions