DataGridViewButtonColumn

来源:互联网 发布:优畅网络是真的吗 编辑:程序博客网 时间:2024/06/16 23:26
.Net的DataGridView控件中,提供了一种列的类型,叫 DataGridViewButtonColumn ,这种列类型是展示为一个 按钮,可以给button赋予相应的text,并且,此button可以用来做处理事件的判断依据。
在正式开始介绍使用方法之前,我们先要进行一个概念性的说明:
DataGridViewButtonColumn,虽然在UI展现上,是一个BUTTON的样子,但是,它的实际形态,并不是传统意义的BUTTON,而是渲染出来的样式,完全是painting的效果而已。所以,对于传统意义的BUTTON的那一套在这里都失效啦

今天,我们先来说一下,如何根据需要动态改变某个button显示的文本

实例1:手动创建


        private void GridViewThree_Load(object sender, EventArgs e)        {            //禁用添加行            dataGridView1.AllowUserToAddRows = false;            //创建 按钮列            DataGridViewButtonColumn btnCol1 = new DataGridViewButtonColumn();            //显示为此列中单元格的按钮文本            btnCol1.UseColumnTextForButtonValue = false;            btnCol1.Name = "Name";            btnCol1.HeaderText = "按钮列^^^^^^";            dataGridView1.Columns.Add(btnCol1);            //创建 多选框列            DataGridViewCheckBoxColumn checkCol = new DataGridViewCheckBoxColumn();            checkCol.HeaderText = "boolean列!!!";            dataGridView1.Columns.Add(checkCol);            //创建 行            DataGridViewRow rowOne = new DataGridViewRow();            rowOne.CreateCells(dataGridView1);            rowOne.Cells[0].Value = "编辑";            rowOne.Cells[1].Value = false;            dataGridView1.Rows.Add(rowOne);            DataGridViewRow rowTwo = new DataGridViewRow();            rowTwo.CreateCells(dataGridView1);            rowTwo.Cells[0].Value = "删除";            rowTwo.Cells[1].Value = true;            dataGridView1.Rows.Add(rowTwo);        }

        //单元格点击事件        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)        {            //获取当前点击的列            DataGridViewButtonColumn btnCol = dataGridView1.Columns[e.ColumnIndex] as DataGridViewButtonColumn;            if (btnCol != null)            {                MessageBox.Show("当前点击的列是:" + btnCol.HeaderText);            }            DataGridViewCheckBoxColumn checkCol = dataGridView1.Columns[e.ColumnIndex] as DataGridViewCheckBoxColumn;            if (checkCol != null)            {                MessageBox.Show("当前点击的是checkbox列:" + checkCol.HeaderText);            }            //e.RowIndex>-1,说明当前点击的不是标题行(列头)            if (e.RowIndex == -1)            {                MessageBox.Show("标题咧");            }            //获取当前点击的行            if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex > -1)            {                //获取当前被点击的单元格                DataGridViewButtonCell btnCell = dataGridView1.CurrentCell as DataGridViewButtonCell;                MessageBox.Show(btnCell.Value.ToString());                if (btnCell.Tag == null)                {                    MessageBox.Show(btnCell.Value.ToString());                    btnCell.Value = "停用";                    btnCell.Tag = true;                }            }        }

实例2:数据绑定


        //自定义绑定,指定标题列        private void DataGridFourth_Load(object sender, EventArgs e)        {            //绑定List集合要求绑定到 对象的属性,而不是成员变量            List<UserName> list = UserName.GetList();            dataGridView1.DataSource = list;        }        //单元格点击事件        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)        {            if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewButtonColumn&&e.RowIndex>-1)            {                DataGridViewButtonCell btnCell = dataGridView1.CurrentCell as DataGridViewButtonCell;                if (btnCell != null)                {                    btnCell.Value = "点击过了";                }            }        }

    public class UserName    {        public string Name { get; set; }        public int Age { get; set; }        public bool IsEnable { get; set; }        public string BtnStr { get; set; }        public static List<UserName> GetList()        {            return new List<UserName>() {               new UserName{Name="张三",Age=20,IsEnable=false,BtnStr="重置"},              new UserName{Name="李四",Age=21,IsEnable=false,BtnStr="重置"},              new UserName{Name="王五",Age=22,IsEnable=true,BtnStr="重置"},              new UserName{Name="找老刘",Age=23,IsEnable=true,BtnStr="重置"}            };        }    }

0 0
原创粉丝点击