Reputation: 4819
I have a project with a form with 3 tabs, each contains 1 datagridview. I disabled multiselect and can only select a row.
I need to Add new data rows or Modify rows of datas from these three datagrids, so I have a MenuStrip on top of the window with a menu with a "Add", "Modify" and "Delete Selected Row" options.
How do I know which row is currently being selected in the current selected tab containing the datagrid which contains that row? Because there are 3 tabs containing 3 datagrids. I don't want to get the output saying there are 3 datagridviews selected with whatever row was selecting. I want the one row on my screen.
Please ask me if my question isn't clear enough.
Upvotes: 1
Views: 1576
Reputation: 32561
I'm assuming that this question is about WinForms
. Below there's a schematic code logic sample for clicking a ToolStripMenuItem
named Show selected row
belonging to a MenuStrip
on the form. Just adapt the method logic to your Modify
and Delete
buttons click
events.
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());
}
}
}
For the .NET Framework 2.0, you can find the selected tab's first DataGridView
control, if existing, like this:
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];
}
}
Upvotes: 2
Reputation: 4430
an approach which does not depend on datagridview's names:
var rtb = ((DataGridView)tabControl1.SelectedTab.Controls.Cast<Control>()
.FirstOrDefault(x => x is DataGridView)).CurrentRow;
(a test to verify that the tabpage has a datagridview might be added )
Upvotes: 0
Reputation: 2899
DataGridViewRow row = new DataGridViewRow();
if (tabControl1.SelectedTab.Name == "Name1")
{
row = dataGridView1.CurrentRow;
}
else
{
if (tabControl1.SelectedTab.Name == "Name2")
{
row = dataGridView2.CurrentRow;
}
else
{
row = dataGridView3.CurrentRow;
}
if (row != null)
{
//your logic here
}
Upvotes: 0
Reputation: 601
In your post back event(s), perhaps check which tab is selected (if statement, case statement) and then take a the specific action to modify your data and page accordingly.
If you want to call another VS generated post back function for the datagrid, you'll have to pass in your parameters correctly - I believe the source should be the row object from your datagrid.
Another option could be to modify your data, re-query, and bind the results to the appropriate grid (rather than trying to use grid events).
Upvotes: 0
Reputation: 2830
Check on click
Suppose Tab Control is tabCtrlReports and Tab name is tbUser then check condition
If (tabCtrlReports.SelectedTab.Name = "tbUser") Then
// your code of grid
End If
In C# just
if (tabCtrlReports.SelectedTab.Name = "tbUser")
{
// your code of grid
}
Upvotes: 1