this is the property | this is the pic
我似乎无法在Datagridview中调整列宽。这是我的代码:
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();
}
我正在尝试从我的表中只获取3列并更改其列宽以适合我的Datagridview。它没有收到任何错误,但也没有改变列宽。
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();
}
答案 0 :(得分:1)
你能尝试这样吗
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;
}