简体   繁体   中英

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:

在此输入图像描述

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!

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

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.

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