簡體   English   中英

C#中DataGridView中的按鈕列?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM