C# 单击 DataGridView 中的行标题的事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11116290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Event for clicking on row headers in DataGridView
提问by l46kok
What is the event that exclusively handles mouse clicks made only on Row Headers of DataGridView?
专门处理仅在 DataGridView 的行标题上进行的鼠标单击的事件是什么?
If there are none, what would be an alternative of handling this type of event?
如果没有,处理此类事件的替代方法是什么?
采纳答案by Angshuman Agarwal
Have a new Winforms Project and copy-paste the code below :-
有一个新的 Winforms 项目并复制粘贴以下代码:-


public partial class Form1 : Form
{
public Form1()
{
var list = new List<Books>
{
new Books() {Title = "Harry Potter", TotalRating = 5},
new Books() {Title = "C#", TotalRating = 5}
};
InitializeComponent();
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = list;
dataGridView1.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(OnRowHeaderMouseClick);
}
void OnRowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
MessageBox.Show("Clicked RowHeader!");
}
}
回答by Brijesh Patel
You can get the row header by following code:
您可以通过以下代码获取行标题:
Private Sub dataGridView1_RowHeaderMouseClick( _
ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) _
Handles dataGridView1.RowHeaderMouseClick
Me.dataGridView1.SelectionMode = _
DataGridViewSelectionMode.RowHeaderSelect
Me.dataGridView1.Rows(e.RowIndex).Selected = True
End Sub
or
或者
void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//
// Do something on double click, except when on the header.
//
if (e.RowIndex == -1)
{
//this is row header...
some code here.
}
Code...
}
回答by Isuru
There are two events related to clicking on Row Headers.
有两个与单击行标题相关的事件。
回答by VEER
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
DataGridViewRow dr = dataGridView1.SelectedRows[0];
this.Hide();
frmStock frm2 = new frmStock();
frm2.Show();
frm2.txtStockID.Text = dr.Cells[0].Value.ToString();
frm2.txtConfigID.Text = dr.Cells[1].Value.ToString();
frm2.txtProductname.Text = dr.Cells[2].Value.ToString();
frm2.txtFeatures.Text = dr.Cells[3].Value.ToString();
frm2.txtPrice.Text = dr.Cells[4].Value.ToString();
frm2.txtQty.Text = dr.Cells[5].Value.ToString();
frm2.txtTotalPrice.Text = dr.Cells[6].Value.ToString();
frm2.dtpStockDate.Text = dr.Cells[7].Value.ToString();
frm2.btnUpdate.Enabled = true;
frm2.btnDelete.Enabled = true;
frm2.btnSave.Enabled = false;
frm2.label8.Text = label1.Text;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

