[英]Button Column in DataGridView in C#?
我試圖將所有按鈕的文本設置為“設置文本”。 我做了這個,但它沒有用......我怎么辦?
foreach (DataGridViewButtonCell btn in dataGridView1.Rows)
{
btn.Text = "Set Text";
}
另外,我如何為這些按鈕設置onclick事件?
嘗試這個:
private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCell cell = row.Cells[0];//Column Index
cell.Value = "Set Text";
}
}
結果:
和對於點擊事件:
private void gw_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)//Column index
{
//Do Somethings
}
}
更改按鈕文字。
const int BUTTON_COLUMN_INDEX = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells[BUTTON_COLUMN_INDEX].Value = "Set Text";
}
即使在DataGridView中也無法單擊按鈕。 相反,您必須處理CellClick
事件並檢查是否單擊了所需的列。
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
// Check if wanted column was clicked
if (e.ColumnIndex == BUTTON_COLUMN_INDEX && e.RowIndex >= 0) {
//Perform on button click code
}
}
嘗試這個
foreach (DataGridViewButtonCell btn in dataGridView1.Rows)
{
btn.Text = "Set Text";
btn.UseColumnTextForButtonValue=true;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.