• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# DataGridViewBand类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中System.Windows.Forms.DataGridViewBand的典型用法代码示例。如果您正苦于以下问题:C# DataGridViewBand类的具体用法?C# DataGridViewBand怎么用?C# DataGridViewBand使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



DataGridViewBand类属于System.Windows.Forms命名空间,在下文中一共展示了DataGridViewBand类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: DataGridViewBandDemo

//引入命名空间
using System.Drawing;
using System.Windows.Forms;
using System;

public class DataGridViewBandDemo : Form
{
    #region "form setup"
    public DataGridViewBandDemo()
    {
        InitializeComponent();

        AddButton(Button1, "Reset",
            new EventHandler(Button1_Click));
        AddButton(Button2, "Change Column 3 Header",
            new EventHandler(Button2_Click));
        AddButton(Button3, "Change Meatloaf Recipe",
            new EventHandler(Button3_Click));
        AddAdditionalButtons();

        InitializeDataGridView();
    }

    DataGridView dataGridView;
    Button Button1 = new Button();
    Button Button2 = new Button();
    Button Button3 = new Button();
    Button Button4 = new Button();
    Button Button5 = new Button();
    Button Button6 = new Button();
    Button Button7 = new Button();
    Button Button8 = new Button();
    Button Button9 = new Button();
    Button Button10 = new Button();
    FlowLayoutPanel FlowLayoutPanel1 = new FlowLayoutPanel();

    private void InitializeComponent()
    {
        FlowLayoutPanel1.Location = new Point(454, 0);
        FlowLayoutPanel1.AutoSize = true;
        FlowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
        AutoSize = true;
        ClientSize = new System.Drawing.Size(614, 360);
        FlowLayoutPanel1.Name = "flowlayoutpanel";
        Controls.Add(this.FlowLayoutPanel1);
        Text = this.GetType().Name;
    }
    #endregion

    #region "setup DataGridView"

    private string thirdColumnHeader = "Main Ingredients";
    private string boringMeatloaf = "ground beef";
    private string boringMeatloafRanking = "*";
    private bool boringRecipe;
    private bool shortMode;

    private void InitializeDataGridView()
    {
        dataGridView = new System.Windows.Forms.DataGridView();
        Controls.Add(dataGridView);
        dataGridView.Size = new Size(300, 200);

        // Create an unbound DataGridView by declaring a
        // column count.
        dataGridView.ColumnCount = 4;
        AdjustDataGridViewSizing();

        // Set the column header style.
        DataGridViewCellStyle columnHeaderStyle =
            new DataGridViewCellStyle();
        columnHeaderStyle.BackColor = Color.Aqua;
        columnHeaderStyle.Font =
            new Font("Verdana", 10, FontStyle.Bold);
        dataGridView.ColumnHeadersDefaultCellStyle =
            columnHeaderStyle;

        // Set the column header names.
        dataGridView.Columns[0].Name = "Recipe";
        dataGridView.Columns[1].Name = "Category";
        dataGridView.Columns[2].Name = thirdColumnHeader;
        dataGridView.Columns[3].Name = "Rating";

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

        foreach (string[] rowArray in rows)
        {
            dataGridView.Rows.Add(rowArray);
        }

        PostRowCreation();

        shortMode = false;
        boringRecipe = true;
    }

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

    // Reset columns to initial disorderly arrangement.
    private void Button1_Click(object sender, System.EventArgs e)
    {
        Controls.Remove(dataGridView);
        dataGridView.Dispose();
        InitializeDataGridView();
    }

    // Change the header in column three.
    private void Button2_Click(object sender,
        System.EventArgs e)
    {
        Toggle(ref shortMode);
        if (shortMode)
        { dataGridView.Columns[2].HeaderText = "S"; }
        else
        { dataGridView.Columns[2].HeaderText = thirdColumnHeader; }
    }

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

    // Change the meatloaf recipe.
    private void Button3_Click(object sender,
        System.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)
    {
        dataGridView.Rows[0].Cells[2].Value = recipe;
        dataGridView.Rows[0].Cells[3].Value = rating;
    }
    #endregion

    #region "demonstration code"
    private void AddAdditionalButtons()
    {
        AddButton(Button4, "Freeze First Row",
            new EventHandler(Button4_Click));
        AddButton(Button5, "Freeze Second Column",
            new EventHandler(Button5_Click));
        AddButton(Button6, "Hide Salad Row",
            new EventHandler(Button6_Click));
        AddButton(Button7, "Disable First Column Resizing",
            new EventHandler(Button7_Click));
        AddButton(Button8, "Make ReadOnly",
            new EventHandler(Button8_Click));
        AddButton(Button9, "Style Using Tag",
            new EventHandler(Button9_Click));
    }

    private void AdjustDataGridViewSizing()
    {
        dataGridView.AutoSizeRowsMode =
            DataGridViewAutoSizeRowsMode.AllCells;
        dataGridView.ColumnHeadersHeightSizeMode = 
            DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    }

    // Freeze the first row.
    private void Button4_Click(object sender, System.EventArgs e)
    {

        FreezeBand(dataGridView.Rows[0]);
    }

    private void Button5_Click(object sender, System.EventArgs e)
    {

        FreezeBand(dataGridView.Columns[1]);
    }

    private static void FreezeBand(DataGridViewBand band)
    {
        band.Frozen = true;
        DataGridViewCellStyle style = new DataGridViewCellStyle();
        style.BackColor = Color.WhiteSmoke;
        band.DefaultCellStyle = style;
    }

    // Hide a band of cells.
    private void Button6_Click(object sender, System.EventArgs e)
    {

        DataGridViewBand band = dataGridView.Rows[3];
        band.Visible = false;
    }

    // Turn off user's ability to resize a column.
    private void Button7_Click(object sender, EventArgs e)
    {

        DataGridViewBand band = dataGridView.Columns[0];
        band.Resizable = DataGridViewTriState.False;
    }

    // Make the entire DataGridView read only.
    private void Button8_Click(object sender, System.EventArgs e)
    {
        foreach (DataGridViewBand band in dataGridView.Columns)
        {
            band.ReadOnly = true;
        }
    }

    private void PostRowCreation()
    {
        SetBandColor(dataGridView.Columns[0], Color.CadetBlue);
        SetBandColor(dataGridView.Rows[1], Color.Coral);
        SetBandColor(dataGridView.Columns[2], Color.DodgerBlue);
    }

    private static void SetBandColor(DataGridViewBand band, Color color)
    {
        band.Tag = color;
    }

    // Color the bands by the value stored in their tag.
    private void Button9_Click(object sender, System.EventArgs e)
    {

        foreach (DataGridViewBand band in dataGridView.Columns)
        {
            if (band.Tag != null)
            {
                band.DefaultCellStyle.BackColor = (Color)band.Tag;
            }
        }

        foreach (DataGridViewBand band in dataGridView.Rows)
        {
            if (band.Tag != null)
            {
                band.DefaultCellStyle.BackColor = (Color)band.Tag;
            }
        }
    }
    #endregion

    [STAThreadAttribute()]
    public static void Main()
    {
        Application.Run(new DataGridViewBandDemo());
    }
}
开发者ID:.NET开发者,项目名称:System.Windows.Forms,代码行数:282,代码来源:DataGridViewBand



注:本文中的System.Windows.Forms.DataGridViewBand类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# DataGridViewBindingCompleteEventArgs类代码示例发布时间:2022-05-26
下一篇:
C# DataGridViewAutoSizeModeEventArgs类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap