如何在C#中的Xml文件中的Tabcontrol中保存所选标签页的内容? [英] How Can I Save The Contents Of The Selected Tabpage In A Tabcontrol In A Xml File In C#?

查看:109
本文介绍了如何在C#中的Xml文件中的Tabcontrol中保存所选标签页的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个tabControl,其中tabPages是在运行时创建的。每个TabPage都包含一个TextBox和一个格式相同的DataGridView。

现在我有一个保存按钮,它应该保存xml文件中当前标签的内容(textbox和datagridview中的值)。 />
但是每当我保存值时,第一个tabPage的内容就会一直保存。我知道我需要使用TabControl.SelectedTab属性,但我无法为其编写代码。

使用下面的代码,我得到以下错误对象引用未设置为实例对象

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"

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 :)

推荐答案





所以在保存事件中,您可以获取当前标签



Hi,

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

TabControl.SelectedTab



但你的挑战会有所不同,就是你会发现每个DataGrid和TextBox,因为每个对象的名称会有所不同。



解决方案:




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 :

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





谢谢

Suvabrata



Thanks
Suvabrata


这篇关于如何在C#中的Xml文件中的Tabcontrol中保存所选标签页的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆