加上这句:
dataGrid.Style["TABLE-LAYOUT"] = "fixed"

解决方案 »

  1.   

    “dataGrid.Style["TABLE-LAYOUT"] = "fixed"“这是在webForm中吧,在winForm中如何实现呢?
      

  2.   

    有两种方法。第一种。是在VS。NET里面直接接设置属性。在DATAGRID的TABLESTYLE里面有一个集合编辑器。你添加一个DATBLESTYLE以后,你在右边会找到一个GRIDCOLUMNSTYLE。在里面你可以做很多东西。包括列宽,READONLY,等等。
    第二种。在DATAGRID的SETDATABINGDING之前加入下面的代码://STEP 1: Create a DataTable style object and set properties if required.
    DataGridTableStyle ts1 = new DataGridTableStyle(); //specify the table from dataset (required step)
    ts1.MappingName = "EastCoastSales";
          
    // Set other properties (optional step)
    ts1.AlternatingBackColor = Color.LightBlue; //STEP 2: Create a string column and add it to the tablestyle
    DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
    TextCol.MappingName = "custName"; //from dataset table
    TextCol.HeaderText = "Customer Name";
    TextCol.Width = 100;
    ts1.GridColumnStyles.Add(TextCol); //STEP 3: Create an int column style and add it to the tablestyle
    //this requires setting the format for the column through its property descriptor
    PropertyDescriptorCollection pdc = this.BindingContext
    [myDataSet, "EastCoastSales"].GetItemProperties(); //now created a formated column using the pdc
    DataGridTextBoxColumn csIDInt = 
    new DataGridTextBoxColumn(pdc["CustID"], "i", true);
    csIDInt.MappingName = "CustID";
    csIDInt.HeaderText = "CustID";
    csIDInt.Width = 50;
    ts1.GridColumnStyles.Add(csIDInt); //STEP 4: Add the checkbox
    DataGridColumnStyle boolCol = new DataGridBoolColumn();
    boolCol.MappingName = "Current";
    boolCol.HeaderText = "Info Current";
    boolCol.Width = 70;
    ts1.GridColumnStyles.Add(boolCol);
    //STEP 5: Add the tablestyle to your datagrid抯 tablestlye collection
    myDataGrid.TableStyles.Add(ts1);