将DataGridView中的数据保存到XmlTextwriter [英] save data in DataGridView to XmlTextwriter

查看:86
本文介绍了将DataGridView中的数据保存到XmlTextwriter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单中有DataGridView.

I have DataGridView in my form.

private void BAddStepLimit_Click(object sender, EventArgs e)
        {
            steplimitdr = spq.steplimitdt.NewRow();
            steplimitdr["stepnumberstlt"] = stepnum;
            steplimitdr["numberstlt"] = dataGridView3.RowCount + 1;
            steplimitdr["normname"] = LNormNameStLt.Text;
            steplimitdr["highwarning"] = NHighWarning.Value;
            steplimitdr["highshutdown"] = NHighshutDown.Value;
            steplimitdr["lowwarning"] = NLowWarning.Value;
            steplimitdr["lowshutdown"] = NLowShutDown.Value;
            steplimitdr["limitholdtime"] = NLimitholdTime.Value;
            if (CBUpperReact.SelectedIndex > -1)
            {
                steplimitdr["upperreact"] = CBUpperReact.Items[CBUpperReact.SelectedIndex];
            }
            if(CBLowerReact.SelectedIndex>-1)
            {
            steplimitdr["lowerreact"] = CBLowerReact.Items[CBLowerReact.SelectedIndex];
            }
            spq.steplimitdt.Rows.Add(steplimitdr);
            bindingSource2.DataSource = spq.steplimitdt;
            dataGridView4.DataSource = bindingSource2.DataSource;
            
        }


然后将其保存到我的xmltextwriter
代码:


and i saved it to my xmltextwriter
the code:

DataTable dt = sq.stepdt;
                        
                        for (int i = 0; i <= dt.Rows.Count-1; i++)
                        {
                            writer.WriteStartElement("Record1");
                            
                            writer.WriteAttributeString("number", tablestep.Rows[i].Cells[0].Value.ToString());
                            writer.WriteAttributeString("stepnumstep", tablestep.Rows[i].Cells[1].Value.ToString());
                            writer.WriteAttributeString("name", tablestep.Rows[i].Cells[2].Value.ToString());
                            writer.WriteAttributeString("description", tablestep.Rows[i].Cells[3].Value.ToString());
                            writer.WriteAttributeString("condition", tablestep.Rows[i].Cells[4].Value.ToString());
                            writer.WriteAttributeString("loopcount", tablestep.Rows[i].Cells[5].Value.ToString());
                            writer.WriteAttributeString("recorder", tablestep.Rows[i].Cells[6].Value.ToString());
                            writer.WriteAttributeString("dyabbreviation", tablestep.Rows[i].Cells[7].Value.ToString());
                            writer.WriteAttributeString("dydynovalue", tablestep.Rows[i].Cells[8].Value.ToString());
                            writer.WriteAttributeString("dyramp", tablestep.Rows[i].Cells[9].Value.ToString());
                            writer.WriteAttributeString("enabbreviation", tablestep.Rows[i].Cells[10].Value.ToString());
                            writer.WriteAttributeString("enenginevalue", tablestep.Rows[i].Cells[11].Value.ToString());
                            writer.WriteAttributeString("enramp", tablestep.Rows[i].Cells[12].Value.ToString());
                            writer.WriteAttributeString("max", tablestep.Rows[i].Cells[13].Value.ToString());
                            writer.WriteAttributeString("holdtime", tablestep.Rows[i].Cells[14].Value.ToString());
                            writer.WriteAttributeString("sample", tablestep.Rows[i].Cells[15].Value.ToString());
                            writer.WriteAttributeString("steptime", tablestep.Rows[i].Cells[16].Value.ToString());
                            writer.WriteEndElement();
                        }

,但不起作用.
请帮帮我.

but dont work.
help me,please.

推荐答案

不要在XML中存储或加载任何控件.相反,要有一个数据层,您可以将其保留为XML.您可以与控件绑定数据层,也可以手动"开发方法来填充/更新UI并根据UI中的更改/事件来更新数据层.

您无需使用低级XML类将数据持久化为XML.您可以使用数据合同自动完成操作,请参见 http://msdn.microsoft .com/en-us/library/ms733127.aspx [ ^ ].

另请参阅我过去的解决方案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [
Don''t store or load any controls to/from XML. Instead, have a data layer which you can persist in XML. You can bind a data layer with you control(s) or "manually" develop methods to populate/update your UI and update data layer based on the changes/events in UI.

You don''t need to persist you data in XML using low-level XML classes. You can do it automatically using Data Contracts, see http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

See also my past solutions:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

—SA


这篇关于将DataGridView中的数据保存到XmlTextwriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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