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;
}
//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());
}
7条答案
按热度按时间busg9geu1#
您需要将数据网格视图的
SelectionMode
设置为FullRowMode
。注意:在Visual Studio 2013和.NET 4.5中,该属性称为
FullRowSelect
。2jcobegt2#
如果希望以编程方式选择行,则应使用datagridview的单元格单击事件:如VB.net和C#中所示
VB.Net
C#
b09cbbtk3#
在DataGridView属性中,设置
lsmd5eda4#
会做出这种事
kq4fsx7k5#
Take a look at My DataGridView
oalqel3c6#
您可以执行以下操作:也许它能帮助你。
mnowg1ta7#
只是为了给予另一个可能的答案,因为这些似乎都不适合我的情况,或者需要额外的事件回调,这是不必要的。