0

4 つの列を持つデータグリッド ビューがあります。

productid 
productname
productprice
buy (this is button column )

ボタン列のクリックイベントを見つけることはできますか? つまり、行 1 ボタンをクリックすると、対応する行の値が別のフォームに転送されます。

行 2 ボタンをクリックすると、対応する値が別のフォームに転送されます。私はWinFormsアプリケーションをやっています。アイデアやサンプルコードをいただければ幸いです。

4

2 に答える 2

2

これは、 DataGridViewButtonColumnの MSDN ページで回答されています。

DataGridView の CellClick または CellContentClick イベントを処理する必要があります。

ハンドラーをアタッチするには:

dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);

そして、イベントを処理するメソッドのコード

void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    // Check that the button column was clicked
    if (dataGridView1.Columns[e.ColumnIndex].Name == "MyButtonColumn")
    {
        // Here you call your method that deals with the row values
        // you can use e.RowIndex to find the row

        // I also use the row's databounditem property to get the bound
        // object from the DataGridView's datasource - this only works with
        // a datasource for the control but 99% of the time you should use 
        // a datasource with this control
        object item = dataGridView1.Rows[e.RowIndex].DataBoundItem;

        // I'm also just leaving item as type object but since you control the form
        // you can usually safely cast to a specific object here.
        YourMethod(item);
    }
}
于 2011-08-29T16:03:44.023 に答える