如何使用自定义控件创建自定义网格视图控件? [英] How to create customised grid view control with customised control?

查看:73
本文介绍了如何使用自定义控件创建自定义网格视图控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to create customised datagridview control with customised control like customised check box. I want to set customised check box as check box column in customised datagridview control and also set customised check box as header of that column. So, how it is possible? I visit lot of web sites but I can't find solution for this so please help me. and I want to do this in c# windows forms application.





我尝试过:



我尝试了很多网站但找不到合适的解决方案



What I have tried:

I tried lot of web sites but can't find proper solution

推荐答案

以下是CodeProject的示例,它有点冗长,但它显示了如何在datagridview中使用复选框: DataGridView Helper Class [ ^ ]



这是一个更简单的例子,你可以使用.png图像跳过这些行:

Here is an example on CodeProject, it is a bit verbose, but it shows how to use checkboxes in a datagridview: DataGridView Helper Class[^]

Here is a simpler example, you can skip the lines using the .png images:
using System;
using System.Windows.Forms;
using System.Drawing;

namespace TestForm1
{
    /// <summary>
    /// DataGridView with buttons and checkboxes.
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            this.InitializeComponent();
        }

        private void buttonFill_Click(object sender, EventArgs e)
        {
            myDataGridView.Columns.Clear();
            myDataGridView.RowHeadersVisible = false;
            //myDataGridView.SortedColumn
            myDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.WhiteSmoke;
            myDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            DataGridViewImageColumn dgvButton = new DataGridViewImageColumn();
            dgvButton.Name = "";
            var image1 = Image.FromFile("expand.png");
            dgvButton.Image = image1;
            myDataGridView.Columns.Add(dgvButton);

            DataGridViewTextBoxColumn dgvText = new DataGridViewTextBoxColumn();
            dgvText.Name = "User ID";
            myDataGridView.Columns.Add(dgvText);

            DataGridViewTextBoxColumn dgvText2 = new DataGridViewTextBoxColumn();
            dgvText2.Name = "Password";
            myDataGridView.Columns.Add(dgvText2);

            DataGridViewComboBoxColumn dgvCombo = new DataGridViewComboBoxColumn();
            dgvCombo.Name = "Priority";
            dgvCombo.Width = 300;
            dgvCombo.DataSource = new string[] { "One", "Two", "Three" };
            myDataGridView.Columns.Add(dgvCombo);

            DataGridViewCheckBoxColumn dgvCheck = new DataGridViewCheckBoxColumn(true);
            myDataGridView.Columns.Add(dgvCheck);

            var row = new object[] { image1, "abc", "xyz", "One", false };
            myDataGridView.Rows.Add(row);
            row = new object[] { image1, "pqr", "stu", "Two", true };
            myDataGridView.Rows.Add(row);
            row = new object[] { image1, "gaga", "aha", "Three", false };
            myDataGridView.Rows.Add(row);
            row = new object[] { image1, "zuzu", "lala", "One", false };
            myDataGridView.Rows.Add(row);
        }

        private void myDataGridView_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                var imageName = myDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag ?? "expand.png";

                if (imageName.ToString() == "toggle.png")
                {
                    imageName = "expand.png";
                }
                else
                {
                    imageName = "toggle.png";
                }

                myDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Image.FromFile(imageName.ToString());
                myDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag = imageName;

                string str = myDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString();
                MessageBox.Show("You Have Selected " + str);
            }
        }

        private void myDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                if (myDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == "abc")
                {
                    //e.CellStyle.BackColor = Color.Yellow;
                    e.CellStyle.ForeColor = Color.Gray;
                }
            }

            if (e.ColumnIndex == 4)
            {
                var check = myDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

                if (check != null && (bool)check == false)
                {
                    e.CellStyle.BackColor = Color.Gray;
                    e.CellStyle.ForeColor = Color.Red;
                }
            }
        }
    }
}


这也可能是一种方法帮助你...



www.codemag.com/article/0707061



它显示了如何为DataGridView创建自定义ColumnType。
This could also be an Approach which helps you ...

www.codemag.com/article/0707061

It shows how to create a custom ColumnType for the DataGridView.


这篇关于如何使用自定义控件创建自定义网格视图控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆