学习DataGridViewX

6 篇文章 0 订阅
列头设置: Columns 集合, name, ColumnType,DataPropertyName. Shape可以选(ButtonXColum)

行字体样式:DefaultCellStyle
行高,复制设置:  RowTemplate

DataGridViewButtonXColumn -- 带有按钮的列,可以在form初始化时候设置按钮的样式:

1.获取按钮
  DataGridViewButtonXColumn bcx =  dataGridViewX1.Columns["Country"] as DataGridViewButtonXColumn;
2. 设置自定义按钮文本
   bcx.UseColumnTextForButtonValue = false;
3. 设置自定义按钮图片 (paintCell事件前, 重绘也是逐行绘制,所以bcx.Text得到是该列某一个cell的值)
   bcx.BeforeCellPaint +=X1Country_BeforeCellPaint;
       void X1Country_BeforeCellPaint(object sender, BeforeCellPaintEventArgs e)
        {
            DataGridViewButtonXColumn bcx = sender as DataGridViewButtonXColumn;

            // Set the button flag image to correspond to country

            if (bcx != null)
                bcx.Image = imageList1.Images[bcx.Text];
        }


DataGridView点击事件: CellContentClick

 dataGridViewX1.CellContentClick += DataGridViewX1_CellContentClick;

 void DataGridViewX1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        // 把列转换为DataGridViewButtonXCell, 如果列类型本身不是DataGridViewXColumn,则cell值为null
            DataGridViewButtonXCell cell = dataGridViewX1.CurrentCell as DataGridViewButtonXCell;

            if (cell != null)
            {
// 根据Cell得到列对象ButtonXColumn
                DataGridViewButtonXColumn bc =
                    dataGridViewX1.Columns[e.ColumnIndex] as DataGridViewButtonXColumn;

                if (bc != null)
                {
                    string s = Convert.ToString(cell.Value);
//得到列名字
                    switch (bc.Name)
                    {
                        case "Country":
                            MessageBox.Show("What a great country " + s + " is!", "",
                                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;

                        case "Region":
                            cell.Value = string.IsNullOrEmpty(s) ? "Global" : "";
                            break;
                    }
                }
            }

        }


            // Update the entire column so that each column cell
                // will be reformatted according to the new list values.
重绘列表的方法
                dataGridViewX1.InvalidateColumn(cell.ColumnIndex);






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值