1、应该可以在某个单元格加contorls,比如datagrid.rows(1)(1).contorls.add()
这个是思路,具体我也没有做过
2.DataGrid1.GridTableStyle.GridColumnStyles[0].Width = 15;

解决方案 »

  1.   

    2.
    //设置datagrid列宽
    dataGrid1.TableStyles.Clear();
    DataGridTableStyle dgts = new DataGridTableStyle(); 
    dgts.MappingName = "用户表"; 
    dataGrid1.TableStyles.Add(dgts); 
    dgts.GridColumnStyles["用户名"].Width  = 80;
    dgts.GridColumnStyles["登录名"].Width  = 80;
    dgts.GridColumnStyles["口令"].Width  = 150;
    dgts.GridColumnStyles["角色ID"].Width  = 50;
      

  2.   

    winForm的话,方法差不多如下:
    5.5 How can I put a combobox in a column of a datagrid?     There are several ways to go about this task. The simplest way involves adding a single combobox to the DataGrid.Controls, and then selectively displaying it as needed when a combobox cell becomes the currentcell. All the work is done in a few event handlers and no overrides or derived classes are necessary. This technique is discussed in Microsoft KB article Q323167.The other techniques require you to derive a columnstyle. Attached is a dropdown combobox sample (C#, VB) that shows how you can use a combobox in a datagrid. This implementation differs from other available columnstyle samples (gotdotnet.com and C# Corner ) in that it derives from DataGridTextBoxColumn. These other samples derive directly from DataGridColumnStyle, and thus have to add functionality that already exists in DataGridTextBoxColumn. This derived DataGridTextBoxColumn does not implement a databound combobox where you can set its DataSource, DisplayMember, and ValueMember to bind the combobox to a foreign table. If you need such a combobox, there is another sample link referenced at the end of this FAQ that does implement such a databound combobox.This sample just attempts to replace the TextBox member of DataGridTextBoxColumn with a standard ComboBox member. Only two overrides need to be handled along with 2 events to get a functional implementation. Here are the notes from the code that list the 3 steps to add a combobox to your datagrid. 
     
         // Step 1. Derive a custom column style from DataGridTextBoxColumn 
     
         //     a) add a ComboBox member 
     
         // b) track when the combobox has focus in Enter and Leave events 
     
         // c) override Edit to allow the ComboBox to replace the TextBox 
     
         // d) override Commit to save the changed data 
       
         // Step 2 - Use the combo column style 
     
         // Add 1 col with combo style 
     
         DataGridComboBoxColumn ComboTextCol = new DataGridComboBoxColumn(); 
     
         ComboTextCol.MappingName = "custCity"; 
     
         ComboTextCol.HeaderText = "Customer Address"; 
     
         ComboTextCol.Width = 100; 
     
         ts1.GridColumnStyles.Add(ComboTextCol); 
      
         // Step 3 - Additional setup for Combo style 
     
         // a) make the row height a little larger to handle minimum combo height 
     
         ts1.PreferredRowHeight = ComboTextCol.ColumnComboBox.Height + 3; 
     
         // b) Populate the combobox somehow. It is a normal combobox, so whatever... 
     
         ComboTextCol.ColumnComboBox.Items.Clear(); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Chicago"); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Corvallis"); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Denver"); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Great Falls"); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Kansas City"); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Los Angeles"); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Raleigh"); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Washington"); 
      
         // c) set the dropdown style of the combo... 
     
         ComboTextCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
     Databound ComboBox Sample
    To use a databound combobox, you have to add overrides for SetColumnValueAtRow and GetColumnValueAtRow to switch the DisplayMember and ValueMember as you get and set the data from the underlying table. Also, you cannot have the ComboBox bound with the same BindingContext to the same datasource as the datagrid. You can download a working project (C#, VB) that implements a databound combobox in a datagrid.Thanks to Gerald Walsh for his suggestion to use the ComboBox.SelectionChangeCommitted event to set the editing flag within our derived columnstyle class. 
     
    WebForm的话,更容易一些,使用模板列
      

  3.   

    winForm的话,方法差不多如下:
    5.5 How can I put a combobox in a column of a datagrid?     There are several ways to go about this task. The simplest way involves adding a single combobox to the DataGrid.Controls, and then selectively displaying it as needed when a combobox cell becomes the currentcell. All the work is done in a few event handlers and no overrides or derived classes are necessary. This technique is discussed in Microsoft KB article Q323167.The other techniques require you to derive a columnstyle. Attached is a dropdown combobox sample (C#, VB) that shows how you can use a combobox in a datagrid. This implementation differs from other available columnstyle samples (gotdotnet.com and C# Corner ) in that it derives from DataGridTextBoxColumn. These other samples derive directly from DataGridColumnStyle, and thus have to add functionality that already exists in DataGridTextBoxColumn. This derived DataGridTextBoxColumn does not implement a databound combobox where you can set its DataSource, DisplayMember, and ValueMember to bind the combobox to a foreign table. If you need such a combobox, there is another sample link referenced at the end of this FAQ that does implement such a databound combobox.This sample just attempts to replace the TextBox member of DataGridTextBoxColumn with a standard ComboBox member. Only two overrides need to be handled along with 2 events to get a functional implementation. Here are the notes from the code that list the 3 steps to add a combobox to your datagrid. 
     
         // Step 1. Derive a custom column style from DataGridTextBoxColumn 
     
         //     a) add a ComboBox member 
     
         // b) track when the combobox has focus in Enter and Leave events 
     
         // c) override Edit to allow the ComboBox to replace the TextBox 
     
         // d) override Commit to save the changed data 
       
         // Step 2 - Use the combo column style 
     
         // Add 1 col with combo style 
     
         DataGridComboBoxColumn ComboTextCol = new DataGridComboBoxColumn(); 
     
         ComboTextCol.MappingName = "custCity"; 
     
         ComboTextCol.HeaderText = "Customer Address"; 
     
         ComboTextCol.Width = 100; 
     
         ts1.GridColumnStyles.Add(ComboTextCol); 
      
         // Step 3 - Additional setup for Combo style 
     
         // a) make the row height a little larger to handle minimum combo height 
     
         ts1.PreferredRowHeight = ComboTextCol.ColumnComboBox.Height + 3; 
     
         // b) Populate the combobox somehow. It is a normal combobox, so whatever... 
     
         ComboTextCol.ColumnComboBox.Items.Clear(); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Chicago"); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Corvallis"); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Denver"); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Great Falls"); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Kansas City"); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Los Angeles"); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Raleigh"); 
     
         ComboTextCol.ColumnComboBox.Items.Add("Washington"); 
      
         // c) set the dropdown style of the combo... 
     
         ComboTextCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
     Databound ComboBox Sample
    To use a databound combobox, you have to add overrides for SetColumnValueAtRow and GetColumnValueAtRow to switch the DisplayMember and ValueMember as you get and set the data from the underlying table. Also, you cannot have the ComboBox bound with the same BindingContext to the same datasource as the datagrid. You can download a working project (C#, VB) that implements a databound combobox in a datagrid.Thanks to Gerald Walsh for his suggestion to use the ComboBox.SelectionChangeCommitted event to set the editing flag within our derived columnstyle class. 
     
    WebForm的话,更容易一些,使用模板列
      

  4.   

    我想到一个问题:如果FORM是可以大小任意改变的,怎么让列表的宽度随着FORM大小的改变成比例改变?
      

  5.   

    在TableStyles属性加入一个TableStyle,假设dGridTS是这个TableStyle,colDPageCode是你要加入datatimepicker控件的列,在窗体加入一个datatimepicker控件datatimepicker1窗体构造函数加入:
    this.colDPageCode.TextBox.Enter+= new System.EventHandler(this.CellPageCode_Enter);
    this.colDPageCode.TextBox.Leave+=new System.EventHandler(this.CellPageCode_Leave);加入事件:
    private void CellPageCode_Enter(object sender, System.EventArgs e)
    {
    dateTimePicker1.Size=((TextBox)sender).Size;
    dateTimePicker1.Location=((TextBox)sender).Location;
    }
    private void CellPageCode_Leave(object sender, System.EventArgs e)
    {
    dGridEdition[dGridEdition.CurrentCell]=dateTimePicker1.Value.ToString();
    }
      

  6.   

    如果FORM是可以大小任意改变的,怎么让列表的宽度随着FORM大小的改变成比例改变?这个可以用百分比得宽度设置来实现,最简单
    把 width 属性设置为 20% 一类的百分比,而不是 20 一类的实际长度,也可以和 table 混合使用。这里的百分比代表所属的容器控件的宽度为100%。
      

  7.   

    如果是随输入的字符串的长度改变呢?把DataGrid中相应的列的item都设置成不换行,这样就会随着字符串的长度改变。在vs.net中,DataGrid控件的属性生成器可以帮助你解决很多问题,去试试,一定收获不小的
      

  8.   

    在vs.net中,DataGrid控件的属性生成器可以帮助你解决很多问题,去试试,一定收获不小的你说的属性生成器在哪里?我找不到
      

  9.   

    CMIC(大象) 
    代码能写的再详细点吗
      

  10.   

    选中那个DataGrid,然后选择TableStyle属性!!
      

  11.   

    第二个问题已经解决了
     CMIC(大象)
    我按你的程序试过拉,调试通过,但是没有任何反应。
    能给我详细的代码吗?谢谢