简体   繁体   中英

Datagridview Column width not changing

this is the property | this is the pic

i cant seem to resize my column width in Datagridview. Here is my code:

      public void dgvwidth()
    {
        crud.FillDataGrid("Select ProductID,BrandName,Dosage from ProductItems", ref dgvOrderproductlist);
        dgvOrderproductlist.Columns[0].Width = 80;
        dgvOrderproductlist.Columns[1].Width = 250;
        dgvOrderproductlist.Columns[2].Width = 80;
    }
      private void HomePage_Load(object sender, EventArgs e)
    {
        dgvwidth();
    }

I'm trying to get just 3 columns from my table and change it column width to fit my Datagridview. its not getting any error but its not changing the column width as well.

    public void FillDataGrid(string sql, ref DataGridView dg)
    {
        try
        {
            DataSet ds = new DataSet();
            cn.Open();
            cmd = new SqlCommand(sql, cn);
            adptr = new SqlDataAdapter(cmd);
            adptr.Fill(ds);
            dg.DataSource = "";
            dg.DataSource = ds.Tables[0];
            dg.AutoResizeColumns();

        }
        catch (Exception e)
        {
            MessageBox.Show("" + e.Message);
        }
        cn.Close();
    }

Can you try like this

 public void dgvwidth()
{
    crud.FillDataGrid("Select ProductID,BrandName,Dosage from ProductItems", ref dgvOrderproductlist);

 var column = dgvOrderproductlist.Columns[0];
 column.Width = 80;

 column  = dgvOrderproductlist.Columns[1];
 column.Width = 250;
  column  =     dgvOrderproductlist.Columns[2];
  column.Width = dgvOrderproductlist.Width - dgvOrderproductlist.Columns[0].Width - dgvOrderproductlist.Columns[1].Width - 50;

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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