如何在Xml文件中一起添加文本框和Datagridview的内容? [英] How Can I Add The Contents Of A Textbox And Datagridview Together In An Xml File?
本文介绍了如何在Xml文件中一起添加文本框和Datagridview的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HI,
我想将textBox和DataGridView的内容一起添加到XML文件中,然后再将其重新加载到同一个文件中。
我知道如何单独添加它们但无法实现此目的。
这就是我添加datagridview的方式,我希望附加文本框的内容
DataTable dt = new DataTable();
foreach (DataGridViewColumn col in dgvIsland.Columns)
{
dt.Columns.Add(col.HeaderText);
}
foreach (DataGridViewRow row in dgvIsland.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);
请帮助!!!
谢谢
解决方案
在你为DataGridView
行做的for循环之后,只需将另一行添加到DataTable
dt。
然后对于那一行,在某些列中,添加TextBox
值。
DataRow newTextBoxRow = dt.NewRow();
newTextBoxRow [ someColumnName] = txtYourTextBox.Text;
HI,
I want to add the contents of a textBox and DataGridView into an XML file together and then again load it back to the same.
I know how to add them separately but cannot achieve this.
This is how i was adding the datagridview and to the same i want to append the contents of the textbox
DataTable dt = new DataTable();
foreach (DataGridViewColumn col in dgvIsland.Columns)
{
dt.Columns.Add(col.HeaderText);
}
foreach (DataGridViewRow row in dgvIsland.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);
Please help!!!
Thanks
解决方案
After the for loop, which you are doing forDataGridView
Rows, just add another Row to theDataTable
dt.
Then for that Row, in some column, add theTextBox
value.
DataRow newTextBoxRow = dt.NewRow(); newTextBoxRow["someColumnName"] = txtYourTextBox.Text;
这篇关于如何在Xml文件中一起添加文本框和Datagridview的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文