我有一种形式以另一种形式调用方法。但是另一种形式的方法不能正常工作。
Form2 调用 main:
private void button1_Click(object sender, EventArgs e)
{
main ma = new main();
ma.AddType(txtName.Text,txtURL.Text,12);
this.Close();
}
main:(向 xml 添加一行并从 xml 重新加载数据网格)
public void AddType(string name, string url, int interval)
{
string path = Application.StartupPath + @"\sites.xml";
//create new instance of XmlDocument
XmlDocument doc = new XmlDocument();
//load from file
doc.Load(path);
//create node and add value
XmlNode node = doc.CreateNode(XmlNodeType.Element, "site", null);
node.InnerXml = "<Name>"+name+"</Name><URL>"+url+"</URL><Status></Status><Response-Time></Response-Time><Last-Checked></Last-Checked>";
//add to elements collection
doc.DocumentElement.AppendChild(node);
//save back
doc.Save(path);
bwLoadXML.RunWorkerAsync();
}
bwLoadXML.RunWorkerAsync(); 由于某种原因,不会在数据网格中显示新的 xml。
编辑,这里是 backgroundWorker:
/////////////////////////////////
////Populate Grid from XML
/////////////////////////////////
private void bwLoadXML_DoWork(object sender, DoWorkEventArgs e)
{
gridPopulate();
}
private void gridPopulate()
{
DataSet data = new DataSet(); string p = System.IO.Path.Combine(Application.StartupPath, "sites.xml");
data.ReadXml(p);
if (this.dataGrid.InvokeRequired)
{
this.dataGrid.Invoke(new MethodInvoker(delegate
{
this.dataGrid.DataSource = data;
this.dataGrid.DataMember = "site";
}));
}
else
{
this.dataGrid.DataSource = data;
this.dataGrid.DataMember = "site";
}
int i = 0;
foreach (DataGridViewColumn column in this.dataGrid.Columns)
{
if (i != 0)
{
if (column.Name == "Name" || column.Name == "Status" || column.Name == "URL" || column.Name == "Response-Time" || column.Name == "Last-Checked")
{
//column.AutoSizeMode
column.Visible = true;
//column.Width = (int)(dataGrid.Width * .2) + (column.Name.Length / 2)-9;
/*if (column.Name == "URL")
{
ColumnHeader ch = new ColumnHeader();
//ch.
}*/
}
else
{
column.Visible = false;
//dataGrid.Columns[i+1].CellType = new DataGridViewButtonColumn();
//dataGrid.Columns[i+1].HeaderCell.
}
}
i++;
}
if (this.dataGrid.InvokeRequired)
{
this.dataGrid.Invoke(new MethodInvoker(delegate
{
// If column 3 is the checkbox column, we sit it's resize mode to none:
dataGrid.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
// Then we set the width:
dataGrid.Columns[0].Width = 25;
dataGrid.Columns[0].DefaultCellStyle.Padding = System.Windows.Forms.Padding.Empty;
// If column 3 is the checkbox column, we sit it's resize mode to none:
dataGrid.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
// Finally we set the rest of the grid to fill or what ever resizing you need:
dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}));
}
else
{
// If column 3 is the checkbox column, we sit it's resize mode to none:
dataGrid.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
// Then we set the width:
dataGrid.Columns[0].Width = 25;
dataGrid.Columns[0].DefaultCellStyle.Padding = System.Windows.Forms.Padding.Empty;
// If column 3 is the checkbox column, we sit it's resize mode to none:
dataGrid.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
// Finally we set the rest of the grid to fill or what ever resizing you need:
dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
}