0

My Form has a fixed size of 1024x600 (dataGridView has 1022 width). When I'm trying to read the XML with this code every Column has the same width and column 10, 11, 12 and 13 are not even shown because they are outside.

private void btLoadXML_Click(object sender, EventArgs e)
    {
    DataTable dt = new DataTable("itemstable");

    dt.Columns.Add("Datum", typeof(System.String));
    dt.Columns.Add("1", typeof(System.String));
    dt.Columns.Add("2", typeof(System.String));
    dt.Columns.Add("3", typeof(System.String));
    dt.Columns.Add("4", typeof(System.String));
    dt.Columns.Add("5", typeof(System.String));
    dt.Columns.Add("6", typeof(System.String));
    dt.Columns.Add("7", typeof(System.String));
    dt.Columns.Add("8", typeof(System.String));
    dt.Columns.Add("9", typeof(System.String));
    dt.Columns.Add("10", typeof(System.String));
    dt.Columns.Add("11", typeof(System.String));
    dt.Columns.Add("12", typeof(System.String));
    dt.Columns.Add("13", typeof(System.String));

    //Read XML File And Display Data in GridView
    dt.ReadXml("items2.xml");
    dataGridView1.DataSource = dt;
    }

How can I adjust the width of each column manually, so everything will fit in?

  • `My Form has a fixed size of 1024x600` - I'd like to see the faces of your end users when they try to run your application in a resolution of say 2560 x 1600 – Federico Berasategui Feb 18 '14 at 18:32

3 Answers3

0

Try this:

dataGridView1.Columns[0].Width = 200;
James
  • 216
  • 1
  • 7
  • Keep in mind that the "0" is the column number and the "200" is a fixed width for that column. You can change the 200 to anything that will work for you. – James Feb 18 '14 at 18:39
0

You should set their width's in the dataGridview like so:

dataGridView1.Columns[index].Width = /*insert number here*/;

Typically, I like to separate the dataTable and dataGridView using the former more for data manipulation and the latter for table rendering.

DDushaj
  • 127
  • 8
0

Use Grid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

For auto-created grids, the only way I could force the columns to start out the right width without responding to an event in the owning control, was:

public static void InitGrid(DataGridView Grid) {
    Grid.HandleCreated+=new System.EventHandler(DoResizeColumnsEvent);
}

static void DoResizeColumnsEvent(object sender,EventArgs e) {
            ((DataGridView)sender).AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);         
}