如何设置通过绑定填充的datagrid的列宽度

主  题:  如何设置通过绑定填充的datagrid的列宽度?
作  者:  gatr ()
等  级:  ^^^^
信 誉 值:  98
所属论坛:  .NET技术 C#
问题点数:  50
回复次数:  2
发表时间:  2003-09-23 15:12:44

我通过数据绑定填充了一个datagrid:

this.dataGridInfo.SetDataBinding(m_DataSet,m_TableName);

可以正确显示数据,也可以用dataGridInfo[i,j].ToString()来读某单元格的内容,但this.dataGridInfo.TableStyles.count确等于0,也就是无法用this.dataGridInfo.TableStyles[0].GridColumnStyles[i].Width来设置宽度,该这类情况该如何设置列宽?窗口为winform.



回复人: smiletosky(仰笑九天) ( 五级(中级)) 信誉:101 2003-09-23 15:26:00 得分:0

DataGridTableStyle dgStyle = new DataGridTableStyle();
dgStyle.MappingName = dtExamset.TableName;
DataGridColumnStyle dcEnddate = new DataGridTextBoxColumn();
dcEnddate.MappingName = "enddate";
dcEnddate.HeaderText = "结束时间";
dcEnddate.Width = 75;
dgStyle.GridColumnStyles.Add(dcEnddate);

this.dgExamset.TableStyles.Add(dgStyle);

回复人: wangj2001(乡村酒吧) ( 一级(初级)) 信誉:100 2003-09-23 15:39:00 得分:0

private void AddCellFormattingColumnStyles(DataGrid grid)
{
    DataGridTableStyle dgts = new DataGridTableStyle();

    DataTable dt = (DataTable)grid.DataSource;

    dgts.MappingName = dt.TableName;

    dt.Columns[0].DataType = typeof(bool);

    DataGridColumnStyle boolCol = new PubLib.XXDataGridBoolColumn(0);   //定义bool型列
    boolCol.MappingName = dt.Columns[0].ColumnName;                     //映射纪录的字段为 :处理标志
    boolCol.HeaderText = "";                                            //列标题
    boolCol.Width = 20;                                                 //列宽
    boolCol.ReadOnly = false;                                           //列的只读书型
    ((DataGridBoolColumn)boolCol).AllowNull = false;                    //当列的值为空时默认值
    dgts.GridColumnStyles.Add(boolCol);

    DataGridColumnStyle TextCol0 = new DataGridTextBoxColumn();
    TextCol0.MappingName = dt.Columns[1].ColumnName; //from dataset table
    TextCol0.HeaderText = dt.Columns[1].ColumnName;
    TextCol0.Width = 100;
    TextCol0.ReadOnly = true;
    dgts.GridColumnStyles.Add(TextCol0);

    DataGridColumnStyle TextCol1 = new DataGridTextBoxColumn();
    TextCol1.MappingName = dt.Columns[2].ColumnName; //from dataset table
    TextCol1.HeaderText = dt.Columns[2].ColumnName;
    TextCol1.Width = 95;
    TextCol1.ReadOnly = true;
    TextCol1.NullText = " ";
    dgts.GridColumnStyles.Add(TextCol1);

    DataGridColumnStyle TextCol13 = new DataGridTextBoxColumn();
    TextCol13.MappingName = dt.Columns[3].ColumnName; //from dataset table
    TextCol13.HeaderText = dt.Columns[3].ColumnName;
    TextCol13.Width = 170;
    TextCol13.ReadOnly = true;
    TextCol13.NullText = " ";
    dgts.GridColumnStyles.Add(TextCol13);

    DataGridColumnStyle TextCol2 = new DataGridTextBoxColumn();
    TextCol2.MappingName = dt.Columns[4].ColumnName; //from dataset table
    TextCol2.HeaderText = dt.Columns[4].ColumnName;
    TextCol2.Width = 40;
    TextCol2.ReadOnly = true;
    TextCol2.NullText = " ";
    dgts.GridColumnStyles.Add(TextCol2);

    DataGridColumnStyle TextCol3 = new DataGridTextBoxColumn();
    TextCol3.MappingName = dt.Columns[5].ColumnName; //from dataset table
    TextCol3.HeaderText = dt.Columns[5].ColumnName;
    TextCol3.Width = 62;
    TextCol3.ReadOnly = true;
    TextCol3.NullText = " ";
    dgts.GridColumnStyles.Add(TextCol3);

    DataGridColumnStyle TextCol5 = new DataGridTextBoxColumn();
    TextCol5.MappingName = dt.Columns[6].ColumnName; //from dataset table
    TextCol5.HeaderText = dt.Columns[6].ColumnName;
    TextCol5.Width = 76;
    TextCol5.ReadOnly = true;
    TextCol5.NullText = " ";                                                //定义当列为空时的默认值
    dgts.GridColumnStyles.Add(TextCol5);

    DataGridTextBoxColumn dgtbc;                                        //定义某一单元格中文本显示属性
    dgtbc = TextCol5 as DataGridTextBoxColumn;
    if (dgtbc != null)
        dgtbc.Format = "yyyy-MM-dd";

    DataGridColumnStyle TextCol4 = new DataGridTextBoxColumn();
    TextCol4.MappingName = dt.Columns[7].ColumnName; //from dataset table
    TextCol4.HeaderText = dt.Columns[7].ColumnName;
    TextCol4.Width = 66;
    TextCol4.ReadOnly = true;
    TextCol4.NullText = " ";
    dgts.GridColumnStyles.Add(TextCol4);

    DataGridColumnStyle TextCol10 = new DataGridTextBoxColumn();
    TextCol10.MappingName = dt.Columns[8].ColumnName; //from dataset table
    TextCol10.HeaderText = dt.Columns[8].ColumnName;
    TextCol10.Width = 130;
    TextCol10.ReadOnly = true;
    TextCol10.NullText = " ";
    dgts.GridColumnStyles.Add(TextCol10);

    DataGridColumnStyle TextCol11 = new DataGridTextBoxColumn();
    TextCol11.MappingName = dt.Columns[9].ColumnName; //from dataset table
    TextCol11.HeaderText = dt.Columns[9].ColumnName;
    TextCol11.Width = 170;
    TextCol11.ReadOnly = true;
    TextCol11.NullText = " ";
    dgts.GridColumnStyles.Add(TextCol11);

    dgts.AlternatingBackColor = Color.Gainsboro;

    grid.TableStyles.Clear();
    grid.TableStyles.Add(dgts);
    CurrencyManager cm = (CurrencyManager)this.BindingContext[grid.DataSource, grid.DataMember];
    ((DataView)cm.List).AllowNew = false;
}
Contributors: FHL