其实,DataGrid就是html中的Table。
如果你用vs.net进行设计,那么DataGrid中每个列都是可以设置的。具体做法如下:
在视图设计器中右键点击DataGrid控件,然后点击“属性生成器”,点击格式->列,然后就可以对各个列进行列宽设置了。
在“格式”这个选项栏里,还有很多设置,自己看看就明白了。

解决方案 »

  1.   

    1.设置列的百分比:
    <asp:BoundColumn DataField="id" "id" >
    <HeaderStyle Width="12%"></HeaderStyle>
    </asp:BoundColumn>
    2.添加滚动条,div
    <div id=div1  WIDTH: 630px">
    <asp:datagrid id="DataGrid1" ....
    </div>
      

  2.   

    To set a column width, your datagrid must be using a non-null DataGridTableStyle. Once this is in place, you can set the column width by first getting the tablestyle and then using that object to obtain a column style with which you can set the width. Here are some code snippets showing how you might do this. 
     
    //.... make sure your DataGrid is using a tablestyle 
     
    dataGrid1.DataSource = _dataSet.Tables["customers"]; 
     
    DataGridTableStyle dgts = new DataGridTableStyle(); 
     
    dgts.MappingName = "customers"; 
     
    dataGrid1.TableStyles.Add(dgts); 
      
    //...... 
      
    //method to set a column with by colnumber 
     
    public void SetColWidth(DataGridTableStyle tableStyle, int colNum, int width) 
     

     
         try 
     
         { 
     
              tableStyle.GridColumnStyles[colNum].Width = width; 
     
              tableStyle.DataGrid.Refresh(); 
     
         } 
     
         catch{} //empty catch .. do nothing 
     

      
    //.... 
      
    // here is how you might call this method 
      
    private void button1_Click(object sender, System.EventArgs e) 
     

     
         DataGridTableStyle tableStyle = dataGrid1.TableStyles["customers"]; 
     
         SetColWidth(tableStyle, 1, 200); 
     

     
      

  3.   

    如果是winform,参考一下这句,我的例子里有10个列,要求宽度相同.tabsty.PreferredColumnWidth=(userdata.ClientSize.Width-tabsty.RowHeaderWidth)/10;当然,也可以使个datagridcolumnstyle的width加起来等于userdata.ClientSize.Width-tabsty.RowHeaderWidth希望能对你有所帮助。
      

  4.   

    如果是webform ,在PreRender控制输出就可以的
      

  5.   

    如果你用vs.net进行设计,那么DataGrid中每个列都是可以设置的。
    在视图设计器中右键点击DataGrid控件,然后点击“属性生成器可以对各个列进行列宽设置了。如果你对html比较熟悉的话,就可以直接对代码进行更改了
      

  6.   

    补充一下是在C#Windows编程中。
      

  7.   

    DataTable dt=new DataTable("aa");
    dt.Columns.Add("ID",typeof(int));
    dt.Columns.Add("NAME",typeof(string));
    dt.Columns.Add("SEX",typeof(string));

    DataRow dr=dt.NewRow();
    dr["id"]=001;
    dr["name"]="宁采臣";
    dr["sex"]="男";
    dt.Rows.Add(dr);
    dr=dt.NewRow();
    dr["id"]=002;
    dr["name"]="小倩";
    dr["sex"]="女";
    dt.Rows.Add(dr);

    DataGridTableStyle dgts=new DataGridTableStyle();
    dgts.MappingName="aa";
    DataGridColumnStyle dgcs=new DataGridTextBoxColumn();
    dgcs.MappingName="NAME";
    dgcs.HeaderText="姓名";
    dgcs.Width=200;
    dgts.GridColumnStyles.Add(dgcs);
    this.dataGrid1.TableStyles.Add(dgts); dgcs=new DataGridTextBoxColumn();
    dgcs.MappingName="SEX";
    dgcs.HeaderText="性别";
    dgcs.Width=30;
    dgts.GridColumnStyles.Add(dgcs);
    this.dataGrid1.TableStyles.Add(dgts); dgcs=new DataGridTextBoxColumn();
    dgcs.MappingName="ID";
    dgcs.HeaderText="序号";
    dgcs.Width=20;
    dgts.GridColumnStyles.Add(dgcs);
    this.dataGrid1.TableStyles.Add(dgts); this.dataGrid1.DataSource=dt;
      

  8.   

    你可以用datagrid的那个很长的那个属性...忘了叫什么了你找找..很长时间没有作cs了....然后你在那里面可以自己设定头..宽度...(那个属性名字很长,,自己找吧)
      

  9.   

    tableStyle.GridColumnStyles[colNum].Width = width;
      

  10.   

    snof(雪狼):
    我没试出你的方法。可能是我那没写对。