Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

I have a tabControl in which tabPages are created at run-time. Every TabPage contains a TextBox and a DataGridView of the same format.
Now i have a save button which should save the content(values in textbox and datagridview) of the current tab in an xml file.
But whenever i am saving the values, the content of the first tabPage is getting saved all the time. I know that i need to use TabControl.SelectedTab property but i am not able to write the code for it.
Using the code below, i get the following error "Object refernce not set to an instance of the object"
C#
DataGridView dgv = null;
            if (profileTabControl.SelectedTab.Controls.ContainsKey("dgvIsland"))
            {
                dgv = (DataGridView)profileTabControl.SelectedTab.Controls["dgvIsland"];
            }
            DataTable dt = new DataTable();
            foreach (DataGridViewColumn col in dgv.Columns)
            {
                dt.Columns.Add(col.HeaderText);
            }
            foreach (DataGridViewRow row in dgv.Rows)
            {
                DataRow dRow = dt.NewRow();
                foreach (DataGridViewCell cell in row.Cells)
                {
                    dRow[cell.ColumnIndex] = cell.Value;
                }
                dt.Rows.Add(dRow);
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            ds.WriteXml(filename);

Can anyone please help in atleast letting me know about how to start with the above problem?

Thanks :)
Posted
Updated 9-Jun-14 3:07am
v2
Comments
Suvabrata Roy 9-Jun-14 8:19am    
You need save only selected Tab or ALL?
sovit agarwal 9-Jun-14 8:25am    
I will be saving all the tabs individually, i.e. if tabPage1 is opened currently then by clicking on SAVE only the current tab will be saved....
Next if TabPage3 is selected, then pressing SAVE button will save the contents of TabPage3 only
Suvabrata Roy 9-Jun-14 8:28am    
Ok, so u need to save selected tab.
Are using AjaxToolKit ?
sovit agarwal 9-Jun-14 8:38am    
No, i am using Winforms
ZurdoDev 9-Jun-14 8:27am    
Please click Improve Question and show us the code you have and why SelectedTab is not working for you.

1 solution

Hi,

so with in the save event you can get the current tab

C#
TabControl.SelectedTab

but your challenge would be something different that is how you will find each DataGrid and TextBox because each object name would be different.

Solution :

C#
foreach(Control Clt in [Your Tab ID].SelectedTab.Controls)
{
   if(Clt is TextBox)
   {
       // Use your operation 
   }
   if(Clt is DataGridView)
   {
      // Use your operation  
   }
}


Thanks
Suvabrata
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900