简体   繁体   English

如何更改表大小以适合数据网格视图大小?

[英]how can I change the table size to be suitable for the data grid view size?

在此处输入图片说明

this is a C# windows form application and am fetching data from database as a data reader object to show it in the data grid view shown in the picture but I can't set the size of the table to be suitable with the data grid view . 这是一个C#Windows窗体应用程序,正在从数据库中读取数据作为数据读取器对象,以便在图片所示的数据网格视图中显示它,但是我不能设置表格的大小以适合数据网格视图。

here's the code : 这是代码:

private void ViewTable()
    {
        SqlConnection con = new SqlConnection("Data Source=Abdullah-PC;Initial Catalog=SmartPharmacyDB;Integrated Security=True");
        SqlCommand com = new SqlCommand();
        com.Connection = con;
        com.CommandText = "select drugname , companyname, price, instock, expirationdate from drugtab order by drugname";
        con.Open();
        SqlDataReader dr = com.ExecuteReader();

        dataGridView1.Columns.Clear();
        dataGridView1.Rows.Clear();
        dataGridView1.Columns.Add("Drug Name", "drugname");
        dataGridView1.Columns.Add("Company Name", "companyname");
        dataGridView1.Columns.Add("Price", "price");
        dataGridView1.Columns.Add("In Stock", "instock");
        dataGridView1.Columns.Add("Expires On", "expirationdate");

        int i = 0;
        while (dr.Read())
        {
            dataGridView1.Rows.Add();
            dataGridView1.Rows[i].Cells[0].Value = dr["drugname"];
            dataGridView1.Rows[i].Cells[1].Value = dr["companyname"];
            dataGridView1.Rows[i].Cells[2].Value = dr["price"];
            dataGridView1.Rows[i].Cells[3].Value = dr["instock"];
            dataGridView1.Rows[i].Cells[4].Value = dr["expirationdate"];
            i++;
        }
        con.Close();
    } 

Do you mean you want the column widths to autosize? 您是说要自动调整列宽吗? There's a property on the DataGridView called AutoSizeColumnsMode. 在DataGridView上有一个名为AutoSizeColumnsMode的属性。 This can be set via the properties Visual Studio panel. 可以通过属性Visual Studio面板设置。 Set this to 'Fill'. 将此设置为“填充”。

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

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