简体   繁体   English

如何根据数据库字段宽度设置Gridview列宽?

[英]How to set Gridview Column width as per database field width?

I am new to windows desktop application development. 我是Windows桌面应用程序开发的新手。

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 : 此表单中的网格显示所有客户的列表,它位于Form_Load()我的代码是:

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. PartyName字段的宽度太短,无法读取全名。 I want to customize the size of the all fields. 我想自定义所有字段的大小。 How to do so? 怎么办?

Please help. 请帮忙。

AutoResizeColumns is what you looking for. 您正在寻找AutoResizeColumns

dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

AllCells - The column width adjusts to fit the contents of all cells in the column, including the header cell. AllCells - 列宽调整以适合列中所有单元格的内容,包括标题单元格。

Also, have a look in DataGridViewAutoSizeColumnMode Enumeration 另外,请查看DataGridViewAutoSizeColumnMode Enumeration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM