如何在datagridview中添加button按鈕
在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)文章
C#中Write()和WriteLine()的區(qū)別分析
這篇文章主要介紹了C#中Write()和WriteLine()的區(qū)別分析,需要的朋友可以參考下2020-11-11C#提示:“在證書存儲區(qū)中找不到清單簽名證書”的解決方法
這篇文章主要介紹了C#提示:“在證書存儲區(qū)中找不到清單簽名證書”的解決方法,分析了幾種常見的解決方案供大家選擇使用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01C#中調(diào)用Windows API的技術(shù)要點(diǎn)說明
本篇文章主要是對C#中調(diào)用Windows API的技術(shù)要點(diǎn)進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01C# MVC模式中應(yīng)該怎樣區(qū)分應(yīng)用程序邏輯(Controller層)和業(yè)務(wù)邏輯(Model層)?
這篇文章主要介紹了C# MVC模式中應(yīng)該怎樣區(qū)分應(yīng)用程序邏輯(Controller層)和業(yè)務(wù)邏輯(Model層)?,這也小編做.NET項(xiàng)目時(shí)經(jīng)常思考和讓人混亂的一個(gè)問題,這篇文章寫的挺好,一下清晰了許多,需要的朋友可以參考下2015-06-06