当前位置:编程学习 > C#/ASP.NET >>

请教一下各位大侠!如何使用代码调整dataGridView单元格的大小!

请教一下各位大侠!如何使用代码调整dataGridView单元格的大小!
dataGridView1.Rows[1].Height = 50;
?????????? --------------------编程问答-------------------- 单元格的大小不就是行和列的大小决定的啊.所以你调整了行高和列宽就可以达到目的了! --------------------编程问答-------------------- 行和列是我用代码生成的,可是我不懂怎么用代码控制列的宽度! --------------------编程问答-------------------- using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

public class ProgrammaticSizing : System.Windows.Forms.Form
{
    private FlowLayoutPanel flowLayoutPanel1;
    private Button button1 = new Button();
    private Button button2 = new Button();
    private Button button3 = new Button();
    private Button button4 = new Button();
    private Button button5 = new Button();
    private Button button6 = new Button();
    private Button button7 = new Button();
    private Button button8 = new Button();
    private Button button9 = new Button();
    private Button button10 = new Button();
    private Button button11 = new Button();

    public ProgrammaticSizing()
    {
        InitializeComponent();
        AddDirections();
        this.Load += new EventHandler(InitializeDataGridView);

        AddButton(button1, "Reset",
            new EventHandler(ResetToDisorder));
        AddButton(button2, "Change Column 3 Header",
            new EventHandler(ChangeColumn3Header));
        AddButton(button3, "Change Meatloaf Recipe",
            new EventHandler(ChangeMeatloafRecipe));
        AddButton(button10, "Change Restaurant 2",
            new EventHandler(ChangeRestaurant));
        AddButtonsForProgrammaticResizing();
    }

    #region form code
    private void InitializeComponent()
    {
        this.flowLayoutPanel1 = new FlowLayoutPanel();
        this.flowLayoutPanel1.FlowDirection
            = FlowDirection.TopDown;
        this.flowLayoutPanel1.Location = new Point(492, 0);
        this.flowLayoutPanel1.AutoSize = true;

        this.AutoSize = true;
        this.Controls.Add(this.flowLayoutPanel1);
        this.Text = this.GetType().Name;
    }

    private void AddDirections()
    {
        Label directions = new Label();
        directions.AutoSize = true;
        String newLine = Environment.NewLine;
        directions.Text = "Press the buttons that start " + newLine
            + "with 'Change' to see how different sizing " + newLine
            + "modes deal with content changes.";

        flowLayoutPanel1.Controls.Add(directions);
    }
    #endregion

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new ProgrammaticSizing());
    }

    private Size startingSize;
    private string thirdColumnHeader = "Main Ingredients";
    private string boringMeatloaf = "ground beef";
    private string boringMeatloafRanking = "*";
    private bool boringRecipe;
    private bool shortMode;
    private DataGridView dataGridView1;
    private string otherRestaurant = "Gomes's Saharan Sushi";

    private void InitializeDataGridView(Object sender,
        EventArgs ignoredToo)
    {
        this.dataGridView1 = new DataGridView();
        this.dataGridView1.Location = new Point(0, 0);
        this.dataGridView1.Size = new Size(292, 266);
        this.Controls.Add(this.dataGridView1);
        startingSize = new Size(450, 400);
        dataGridView1.Size = startingSize;

        AddColumns();
        PopulateRows();

        shortMode = false;
        boringRecipe = true;
    }

    private void AddColumns()
    {
        dataGridView1.ColumnCount = 4;
        dataGridView1.ColumnHeadersVisible = true;

        DataGridViewCellStyle columnHeaderStyle =
            new DataGridViewCellStyle();

        columnHeaderStyle.BackColor = Color.Aqua;
        columnHeaderStyle.Font = new Font("Verdana", 10,
            FontStyle.Bold);
        dataGridView1.ColumnHeadersDefaultCellStyle =
            columnHeaderStyle;

        dataGridView1.Columns[0].Name = "Recipe";
        dataGridView1.Columns[1].Name = "Category";
        dataGridView1.Columns[2].Name = thirdColumnHeader;
        dataGridView1.Columns[3].Name = "Rating";
    }

    private void PopulateRows()
    {
        string[] row1 = {
            "Meatloaf", "Main Dish", 
            boringMeatloaf, boringMeatloafRanking
        };
        string[] row2 = {
            "Key Lime Pie", "Dessert", 
            "lime juice, evaporated milk", "****"
        };
        string[] row3 = {
            "Orange-Salsa Pork Chops", "Main Dish", 
            "pork chops, salsa, orange juice", "****"
        };
        string[] row4 = {
            "Black Bean and Rice Salad", "Salad", 
            "black beans, brown rice", "****"
        };
        string[] row5 = {
            "Chocolate Cheesecake", "Dessert", 
            "cream cheese", "***"
        };
        string[] row6 = {
            "Black Bean Dip", 
            "Appetizer", "black beans, sour cream", "***"
        };
        object[] rows = new object[] {
            row1, row2, row3, row4, row5, row6
        };

        foreach (string[] row in rows)
            dataGridView1.Rows.Add(row);
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (row.IsNewRow) break;
            row.HeaderCell.Value = "Restaurant " + row.Index;
        }
    }

    private void AddButton(Button button, string buttonLabel,
        EventHandler handler)
    {
        button.Text = buttonLabel;
        button.AutoSize = true;
        flowLayoutPanel1.Controls.Add(button);
        button.Click += handler;
    }

    private void ResetToDisorder(Object sender, EventArgs e)
    {
        Controls.Remove(dataGridView1);
        dataGridView1.Size = startingSize;
        dataGridView1.Dispose();
        InitializeDataGridView(null, null);
    }

--------------------编程问答--------------------
    private void ChangeColumn3Header(Object sender, EventArgs e)
    {
        Toggle(ref shortMode);
        if (shortMode)
            dataGridView1.Columns[2].HeaderText = "S";
        else
            dataGridView1.Columns[2].HeaderText =
                thirdColumnHeader;
    }

    private static void Toggle(ref Boolean toggleThis)
    {
        toggleThis = !toggleThis;
    }

    private void ChangeMeatloafRecipe(Object sender,
        EventArgs e)
    {
        Toggle(ref boringRecipe);

        if (boringRecipe)
            SetMeatloaf(boringMeatloaf, boringMeatloafRanking);
        else
        {
            string greatMeatloafRecipe =
                "1 lb. lean ground beef, "
                + "1/2 cup bread crumbs, 1/4 cup ketchup,"
                + "1/3 tsp onion powder, "
                + "1 clove of garlic, 1/2 pack onion soup mix "
                + " dash of your favorite BBQ Sauce";

            SetMeatloaf(greatMeatloafRecipe, "***");
        }
    }

    private void SetMeatloaf(string recipe, string rating)
    {
        dataGridView1.Rows[0].Cells[2].Value = recipe;
        dataGridView1.Rows[0].Cells[3].Value = rating;
    }

    private void ChangeRestaurant(Object sender,
        EventArgs ignored)
    {
        if (dataGridView1.Rows[2].HeaderCell.Value.Equals(otherRestaurant))
        {
            dataGridView1.Rows[2].HeaderCell.Value =
                "Restaurant 2";
        }
        else
        {
            dataGridView1.Rows[2].HeaderCell.Value = otherRestaurant;
        }
    }

    #region "programmatic resizing"

    private void AddButtonsForProgrammaticResizing()
    {
        AddButton(button4, "Size Third Column",
            new EventHandler(SizeThirdColumnHeader));
        AddButton(button5, "Size Column Headers",
            new EventHandler(SizeColumnHeaders));
        AddButton(button6, "Size All Columns",
            new EventHandler(SizeAllColumns));
        AddButton(button7, "Size Third Row",
            new EventHandler(SizeThirdRow));
        AddButton(button8, "Size First Row Header Using All Headers",
            new EventHandler(SizeFirstRowHeaderToAllHeaders));
        AddButton(button9, "Size All Rows and Row Headers",
            new EventHandler(SizeAllRowsAndTheirHeaders));
        AddButton(button11, "Size All Rows ",
            new EventHandler(SizeAllRows));
    }

    private void SizeThirdColumnHeader(Object sender,
        EventArgs e)
    {
        dataGridView1.AutoResizeColumn(
            2, DataGridViewAutoSizeColumnMode.ColumnHeader);
    }

    private void SizeColumnHeaders(Object sender, EventArgs e)
    {
        dataGridView1.AutoResizeColumnHeadersHeight(2);
    }

    private void SizeAllColumns(Object sender, EventArgs e)
    {
        dataGridView1.AutoResizeColumns(
            DataGridViewAutoSizeColumnsMode.AllCells);
    }

    private void SizeThirdRow(Object sender, EventArgs e)
    {
        dataGridView1.AutoResizeRow(
            2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader);
    }

    private void SizeFirstRowHeaderToAllHeaders(Object sender, EventArgs e)
    {
        dataGridView1.AutoResizeRowHeadersWidth(
            0, DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
    }

    private void SizeAllRowsAndTheirHeaders(Object sender, EventArgs e)
    {
        dataGridView1.AutoResizeRows(
            DataGridViewAutoSizeRowsMode.AllCells);
    }

    private void SizeAllRows(Object sender,
        EventArgs e)
    {
        dataGridView1.AutoResizeRows(
            DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders);
    }
    #endregion
} --------------------编程问答-------------------- 原文引用   http://msdn2.microsoft.com/zh-cn/library/ff173xd4(VS.80).aspx --------------------编程问答-------------------- 好可怕哦,那么多代码呀 --------------------编程问答-------------------- dataGridView1.Rows[1].Height  =50;  你这样只是改了行高  而不是单元格
dataGridView1.Rows[1][0].Width =50;  这样试试看
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,