string query;
query = "....SQL...";
{
conn.Open();
using (OracleDataAdapter a = new OracleDataAdapter(query, conn))
{
DataTable t = new DataTable();
a.Fill(t);
dgSku.TableStyles.Clear();
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = t.TableName;
foreach (DataColumn item in t.Columns)
{
DataGridTextBoxColumn tbcName = new DataGridTextBoxColumn();
tbcName.Width = 80;
tbcName.MappingName = item.ColumnName;
tbcName.HeaderText = item.ColumnName;
tableStyle.GridColumnStyles.Add(tbcName);
}
dgSku.TableStyles.Add(tableStyle);
}
}
DataGrid column size (Compact Framework) C#
-
02-06-2022 - |
Question
I'm new with DataGrids.
My Code:
private void populateGrid()
{
conn.Open();
string query;
query = "select company_id_no, company_name, last_update_datetime, username from company";
OracleDataAdapter da = new OracleDataAdapter(query, conn);
OracleDataSet ds = new OracleDataSet();
da.Fill(ds);
dgSku.DataSource = ds.Tables[0];
}
This is how it looks on my mobile device:
I want it to automatically re-size the columns to 100%, like:
I would really appreciate if someone can point me in the right direction.
Thanks in advance!
Solution
OTHER TIPS
I don't think there is an automatic sizing property.
This article show you how to use a DataGridTableStyle to adjust the width of a column.
http://support.microsoft.com/kb/330610
If you combine this with the Graphics.MeasureString method you should be able to calculate the required column size.
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.measurestring(v=vs.71).aspx
Personally I would just set some sensible defaults on the column widths using a DataGridTableStyle and forget about the auto sizing. It becomes a particular pain when you have to start taking into account space for an up/down scroll bar and screen rotation.