有哪个事件专门处理 DataGridView 中行标题的鼠标点击?
如果没有,有什么替代方法可以处理这种事件?
有哪个事件专门处理 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);
}
}