55

ユーザーが任意のセルをクリックするdataGridViewと、このセルを含む行全体も選択される必要があります。(複数選択無効になっています)このようなものを取得してみcurrentRowIndexました

 int Index = dataGridView1.CurrentCell.RowIndex;

ただし、その行を選択するためにインデックスを使用する方法がわかりません。これと他の6つの方法について試してみましたが、成功しませんでした:

dataGridView1.Select(Index);

これを行う方法を知っていますか?

4

6 に答える 6

108

datagridview を に設定する必要がありSelectionModeますFullRowMode

注: .NET 4.5 を使用する Visual Studio 2013 では、プロパティは と呼ばれFullRowSelectます。

于 2012-12-02T19:11:14.877 に答える
9

行をプログラムで選択したい場合は、datagridview のセル クリック イベントを使用します。VB.net と C# で示されています。

VB.Net

Private Sub dgvGrid_CellClick(sender as System.Object, e as System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvGrid.CellClick
    If e.RowIndex < 0 Then
        Exit Sub
    End If

    intIndex = e.RowIndex
    dgvGrid.Rows(intIndex).Selected = True
Exit Sub

C#

private void dgvRptTables_CellClick(System.Object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
    if (e.RowIndex < 0) {
        return;
    }

    int index = e.RowIndex;
    dgvGrid.Rows[index].Selected = true;
}
于 2012-12-03T10:53:33.973 に答える
0
//class to store ID (Pri. Key) value of selected row from DataGridView
public class Variables
{
   public static string StudentID;
}                                  

//This is the event call on cell click of the DataGridView
private void dataGridViewDisplay_CellClick(object sender, DataGridViewCellEventArgs e)
{
   Variables.StudentID =this.dataGridViewDisplay.CurrentRow.Cells[0].Value.ToString();
//textBoxName is my form field where I set the value of Name Column from the Selected row from my DataGridView 

   textBoxName.Text = this.dataGridViewDisplay.CurrentRow.Cells[1].Value.ToString();

   dateTimePickerDOB.Value = Convert.ToDateTime(this.dataGridViewDisplay.CurrentRow.Cells[2].Value.ToString());
}

My DataGridView を見てください

于 2018-08-16T14:30:09.357 に答える