我假设这个问题是关于WinForms
. 下面有一个示意性的代码逻辑示例,用于单击表单上属于a 的ToolStripMenuItem
命名。只需将方法逻辑调整到您的和按钮事件即可。Show selected row
MenuStrip
Modify
Delete
click
public Form1()
{
InitializeComponent();
dataGridView1.DataSource = new[] { new { Id = 1 }, new { Id = 10 } };
dataGridView2.DataSource = new[] { new { Id = 2 }, new { Id = 20 } };
dataGridView3.DataSource = new[] { new { Id = 3 }, new { Id = 30 } };
}
private void showSelectedRowToolStripMenuItem_Click(object sender, EventArgs e)
{
var dgv = tabControl1.SelectedTab.Controls.OfType<DataGridView>().FirstOrDefault();
if(dgv != null)
{
if (dgv.SelectedRows.Count > 0)
{
// I'm using MessageBox to show the index of the row.
// You should add your Modify / Delete logic
MessageBox.Show(dgv.SelectedRows[0].Index.ToString());
}
}
}
对于 .NET Framework 2.0,您可以找到所选选项卡的第一个DataGridView
控件(如果存在),如下所示:
DataGridView dgv = null;
for (int i = 0; i < tabControl1.SelectedTab.Controls.Count; i++)
{
if (tabControl1.SelectedTab.Controls[i].GetType() == typeof(DataGridView))
{
dgv = (DataGridView)tabControl1.SelectedTab.Controls[i];
}
}