Search code examples
c#datagridwindows-mobilecompact-frameworkwindows-ce

DataGrid column size (Compact Framework) C#


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:

enter image description here

I want it to automatically re-size the columns to 100%, like:

enter image description here

I would really appreciate if someone can point me in the right direction.

Thanks in advance!


Solution

  •         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);
                }
            }