0

I am new to windows desktop application development.

I have a grid in which I am displaying list of customers whose bills are printed.

The form looks like this.

The grid in this form displays the list of all customers and it is bounded in the Form_Load() my code is :

private void SearchForm_Load(object sender, EventArgs e)
{   
    cn = db.createConnection();
    if (cn.State == System.Data.ConnectionState.Open)
        cn.Close();
    cn.Open();
    cmd = new OleDbCommand("Select BillNo,PartyName,Address,City,State,BillDt from BillMaster", cn);
    da = new OleDbDataAdapter(cmd);
    ds = new DataSet();
    da.Fill(ds);
    cn.Close();
    dataGridView1.DataSource = ds.Tables[0];
    ds.Dispose();
}

But the width of the PartyName field is too short to read the full name. I want to customize the size of the all fields. How to do so?

Please help.

torrential coding
  • 1,755
  • 2
  • 24
  • 34
Mohemmad K
  • 809
  • 7
  • 33
  • 74
  • Do you want the width the be dynamic ? i.e. to match longest string ? – Ofiris Apr 15 '13 at 14:39
  • Yes Sir, I want to do same for all fields. @Ofiris – Mohemmad K Apr 15 '13 at 14:41
  • Ok, check my answer and have a look in http://stackoverflow.com/questions/1025670/how-do-you-automatically-resize-columns-in-a-datagridview-control-and-allow-the for more options. – Ofiris Apr 15 '13 at 14:49

1 Answers1

2

AutoResizeColumns is what you looking for.

dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

AllCells - The column width adjusts to fit the contents of all cells in the column, including the header cell.

Also, have a look in DataGridViewAutoSizeColumnMode Enumeration

Ofiris
  • 6,047
  • 6
  • 35
  • 58