this.dataGridView1.Columns[0].Width = 50;

解决方案 »

  1.   

                dataGridView1.Columns[0].Width = 50;
                dataGridView1.Columns[1].Width = 150;
      

  2.   

    给你一个自动伸缩的函数
    private  void SizeColumnsToContent(DataGrid dataGrid, int nRowsToScan) 
    {
    // Create graphics object for measuring widths.
    Graphics Graphics = dataGrid.CreateGraphics();
        
    // Define new table style.
    DataGridTableStyle tableStyle = new DataGridTableStyle();
        
    try
    {
    DataTable dataTable = (DataTable)dataGrid.DataSource;
          
    if (-1 == nRowsToScan)
    {
    nRowsToScan = dataTable.Rows.Count;
    }
    else
    {
    // Can only scan rows if they exist.
    nRowsToScan = System.Math.Min(nRowsToScan, dataTable.Rows.Count);
    }
                  
    // Clear any existing table styles.
    dataGrid.TableStyles.Clear();
          
    // Use mapping name that is defined in the data source.
    tableStyle.MappingName = dataTable.TableName;
          
    // Now create the column styles within the table style.
    DataGridTextBoxColumn columnStyle;
    int iWidth;
         
      for (int iCurrCol = 0; iCurrCol < dataTable.Columns.Count; iCurrCol++)
    {
    DataColumn dataColumn = dataTable.Columns[iCurrCol];
    columnStyle = new DataGridTextBoxColumn(); //columnStyle.HeaderText .Enabled = true;
    columnStyle.HeaderText = dataColumn.ColumnName;
    columnStyle.MappingName = dataColumn.ColumnName;
            
    // Set width to header text width.
    iWidth = (int)(Graphics.MeasureString(columnStyle.HeaderText, dataGrid.Font).Width);
     
       // Change width, if data width is wider than header text width.
    // Check the width of the data in the first X rows.
    DataRow dataRow;
    for (int iRow = 0; iRow < nRowsToScan; iRow++)
    {
    dataRow = dataTable.Rows[iRow];
              
    if (null != dataRow[dataColumn.ColumnName])
    {
    int iColWidth = (int)(Graphics.MeasureString(dataRow.ItemArray[iCurrCol].ToString(), dataGrid.Font).Width);
    int iColHight = (int)(Graphics.MeasureString(dataRow.ItemArray[iCurrCol].ToString(), dataGrid.Font).Height );
    iWidth = (int)System.Math.Max(iWidth, iColWidth);

    }
    }
    columnStyle.Width = iWidth + 4;
      
    tableStyle.GridColumnStyles.Add(columnStyle);
    }
    // Add the new table style to the data grid.
    dataGrid.TableStyles.Add(tableStyle);
    }
    catch(Exception e)
    {
    MessageBox.Show(e.Message);
    }
    finally
    {
    Graphics.Dispose();
    }
    }
      

  3.   

    这样的话PerferredColumnWidth属性怎么设置
      

  4.   

    1.LZ,7楼的解决方案还不能满足你的问题要求吗?2.GOOD ANSWER 
    I'm trying to update a datagrid's column preferredcolumnwidth...Am i missing something, this doesnt seem to be working. It works when I
    place it in the Form1_Loan event function but not in the form resize
    function.private void Form1_Resize(object sender, System.EventArgs e)
    {
    int dataGridWidth = Form1.ActiveForm.Width - 35;
    dataGrid1.Width = dataGridWidth;
    int eachRowSize = (dataGridWidth) / 7;
    dataGrid1.PreferredColumnWidth = eachRowSize;
    textBox1.Text = dataGridWidth.ToString();
    textBox1.Update();dataGrid1.PreferredColumnWidth = eachRowSize;
    //dataGrid1.Update();}