欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何在datagridview中添加button按鈕

 更新時(shí)間:2023年09月01日 09:18:15   作者:木子松的貓  
這篇文章主要介紹了如何在datagridview中添加button按鈕問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

在datagridview中添加button按鈕

.Net的DataGridView控件中,提供了一種列的類型,叫 DataGridViewButtonColumn ,這種列類型是展示為一個(gè) 按鈕,可以給button賦予相應(yīng)的text,并且,此button可以用來做處理事件的判斷依據(jù)。

DataGridViewButtonColumn,雖然在UI展現(xiàn)上,是一個(gè)BUTTON的樣子,但是,它的實(shí)際形態(tài),并不是傳統(tǒng)意義的BUTTON,而是渲染出來的樣式,完全是painting的效果而已。

所以,對于傳統(tǒng)意義的BUTTON的那一套在這里都失效啦

代碼實(shí)現(xiàn)

//在datagridview中添加button按鈕
            DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            btn.Name = "btnModify";
            btn.HeaderText = "修改";
            btn.DefaultCellStyle.NullValue = "修改";
            dataGridView1.Columns.Add(btn);  

然后在DataGridView的CellContentClick事件中寫類似如下代碼:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //點(diǎn)擊button按鈕事件
            if (dataGridView1.Columns[e.ColumnIndex].Name == "btnModify" && e.RowIndex >= 0)
            {
            //說明點(diǎn)擊的列是DataGridViewButtonColumn列
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                IPEntity ipentity = new IPEntity();
                ipentity.ipName = Convert.ToString(dataGridView1.CurrentRow.Cells[0].Value);
                ipentity.ipPart = Convert.ToString(dataGridView1.CurrentRow.Cells[1].Value);
                ipentity.ipStart = Convert.ToString(dataGridView1.CurrentRow.Cells[2].Value);
                ipentity.ipEnd = Convert.ToString(dataGridView1.CurrentRow.Cells[3].Value);
                bool flag = selectIp.UpdateIP(ipentity);
                if (flag)
                {
                    MessageBox.Show("更新成功!");
                }
                else
                {
                    MessageBox.Show("更新失??!");
                }
            }           
        }

效果圖如下:

datagridview中按鈕不顯示文本

向datagridview中添加按鈕的方法很簡單。

首先,選中datagridview控件,點(diǎn)擊右鍵,選擇“編輯列”。在彈出的“編輯列”窗體中選擇我們剛剛添加的按鈕,找到”DefaultCellStyle“屬性,點(diǎn)擊打開。

在打開的窗體中,找到NullValue屬性,將文本設(shè)置為想要實(shí)現(xiàn)的內(nèi)容就可以了。

我們這里輸入的是”審核“,然后點(diǎn)擊”確定“按鈕。

最終的效果如下,

可以很清楚地看到按鈕上顯示的文本是”審核“了。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論