Reputation: 1154
I have created data grid view like this:
I want to change the column width. What should I do? Should I change the code in designer.cs or just in .cs?
Update:
private void sqlConnStaff()
{
BindingSource dbBindSource = new BindingSource();
SqlCommand com;
com = new SqlCommand();
SqlConnection con = new SqlConnection(strCon);
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "view_staff";
SqlDataAdapter dataAdapter = new SqlDataAdapter(com);
IDCabang = new SqlParameter();
IDCabang.SqlDbType = SqlDbType.VarChar;
IDCabang.Size = 5;
IDCabang.ParameterName = "@IDCabang";
IDCabang.Value = IDCabangC;
IDCabang.Direction = ParameterDirection.Input;
com.Parameters.Add(IDCabang);
con.Open();
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
dbBindSource.DataSource = table;
dataGridView3.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
// you can make it grid readonly.
dataGridView3.ReadOnly = true;
// finally bind the data to the grid
dataGridView3.DataSource = dbBindSource;
con.Close();
}
Upvotes: 2
Views: 12438
Reputation: 98858
Check out DataGridViewColumn.Width
property.
Gets or sets the current width of the column.
DataGridViewColumn firstcolumn = dataGridView1.Columns[0];
column.Width = 150;
You can set your .Width
property as a pixel, default value is 100
.
You can change it if you want to use in Design View as Habib pointed.
Upvotes: 1
Reputation: 10055
Sorry i cant see the image. Is blocked by my work firewall but if you want to change the width i would advise never to modify the designer.cs
The designer.cs is auto generated code and people just assume this is not modified so it could become a pain in the ass to fix at a later date if needed.
Modify it in the .cs files.
Upvotes: 0
Reputation: 223392
You can set the Width property on the grid column like:
dataGridView1.Columns[0].Width = 200;
Or you can set the Width of the column in designer, modifying desinger.cs
is generally not recommended, instead go the Design View there modify the column width property.
If you are binding the resultset from code and creating the column as well from the (code based on the resultset) then you have to specify / modify the width in the code. If you are designing the grid in design view and adding column from there then define the width in Design view. Go to DataGrid properties, Columns -> Add new Column there modify the width:
Upvotes: 2
Reputation: 17600
You can change it in designer: click on DataGridView
then on this small arrow in upper right corner of gird and go Edit Columns -> Select Column -> Width
default is 100.
Upvotes: 0