DataGrid column size (Compact Framework) C#

两盒软妹~` 提交于 2019-12-11 01:47:58

问题


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!


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/17590211/datagrid-column-size-compact-framework-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!