我是c#的初学者。我有一个 XML file (xmlfile.xml) 其中包含一些文本。
在Form Load事件中,我正在读取XML文件并将其显示在 datagridview1 属于 mainForm 类型
datagridview1
mainForm
我使用以下代码来完成此操作
DataSet ds = new DataSet(); ds.ReadXml(@"D:\xmlfile.xml"); dataGridView1.DataSource = ds.Tables[0].DefaultView; dataGridView1.Tag = ds; /* TAG */
我有另一张表格 noteForm 我有 textBox1 和 btnSub 按钮每当用户点击 b子系统 按钮 textBox1.Text 应添加到 dataGridView1 的 主窗体
noteForm
textBox1
btnSub
b子系统
textBox1.Text
dataGridView1
主窗体
我正在尝试以下代码来完成此操作
string strTitle; string[] row = new string[] { strTitle, DateTime.Now.ToString("M/d/y"), "checked" }; _parent.dataGridView1.Tag.Tables[0].Rows.Add(row); _parent.dataGridView1.Rows.Add(); _parent.dataGridView1.DataSource = _parent.dataGridView1.Tag.Tables[0].DefaultView;
上面的代码显示错误--“当控件是数据绑定的时,无法以编程方式将行添加到DataGridView的行集合中。”
我不明白这个错误是什么。请帮忙。
_家长只是 主窗体 形式 注释窗体
注释窗体
将新行添加到数据集中 ds 不去 dataGridView1
ds
ds.Tables[0].Rows.Add(row)
编辑
string strTitle; string[] row = new string[] { strTitle, DateTime.Now.ToString("M/d/y"), "checked" }; DataSet ds = (DataSet)_parent.dataGridView1.Tag; ds.Tables[0].Rows.Add(row);