专门处理仅在 DataGridView 的行标题上进行的鼠标单击的事件是什么?
如果没有,那么处理此类事件的替代方法是什么?
有一个新的 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!");
}
}
您可以通过以下代码获取行标题:
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
或者
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...
}
有两个与单击行标题相关的事件。
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);
}
}