我有一个包含许多列和行的DataGridView,用户可以右键单击单元格并从ContextMenuStrip中选择一个选项。选项有红色、蓝色、绿色等颜色,如果用户选择(例如)红色,则所选单元格将其BackColor设置为红色,并且用户还可以在该单元格中写入值。好吧,我的问题是,我找不到保存所有内容和样式的方法,所以如果用户重新打开for,dataGridView将有其最后的设置(包括单元格的BackColor和ForeColor)。
我试着这样保存内容,它给了我错误,我不知道如何打开它。
private void button4_Click(object sender, EventArgs e)
{
SaveFileDialog svd = new SaveFileDialog();
svd.Filter = "XML Files (*.xml)|*.xml";
if(svd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
DataTable dt = ((DataView)this.dataGridView1.DataSource).Table;
dt.WriteXml(svd.FileName);
}
}
如果有更好的方法来保存内容和风格,也欢迎。提前感谢
如果您只需要持久化单元格格式,那么以下类型的对象序列化将适用于您。
创建一个类来序列化您想要的属性:
public class SavedSettings
{
[XmlIgnore]
public Color ForeColor { get; set; }
[XmlElement("ForeColor")]
public int ForeColorARGB
{
get { return this.ForeColor.ToArgb(); }
set { this.ForeColor = Color.FromArgb(value); }
}
[XmlIgnore]
public Color BackColor { get; set; }
[XmlElement("BackColor")]
public int BackColorARGB
{
get { return this.BackColor.ToArgb(); }
set { this.BackColor = Color.FromArgb(value); }
}
public object Value { get; set; }
}
在您的主类中,从xml:加载任何保存的设置
public List<SavedSettings> Settings { get; set; }
private void ReadXML()
{
System.Xml.Serialization.XmlSerializer reader =
new System.Xml.Serialization.XmlSerializer(typeof(List<SavedSettings>));
if (File.Exists(@"SavedSettings.xml"))
{
System.IO.StreamReader file = new System.IO.StreamReader(
@"SavedSettings.xml");
this.Settings = (List<SavedSettings>)reader.Deserialize(file);
file.Close();
}
}
private void LoadDGV()
{
this.ReadXML();
if (this.Settings != null)
{
// This assumes your dgv has added columns already.
int rows = this.Settings.Count / this.dataGridView1.Columns.Count;
int cols = this.dataGridView1.Columns.Count;
this.dataGridView1.Rows.AddCopies(0, rows);
for (int i = 0; i < this.Settings.Count; i++)
{
int row = i / cols;
int col = i % cols;
this.dataGridView1[col, row].Style.BackColor = this.Settings[i].BackColor;
this.dataGridView1[col, row].Style.ForeColor = this.Settings[i].ForeColor;
this.dataGridView1[col, row].Value = this.Settings[i].Value;
}
}
}
然后,当您准备好保存时,将单元格设置重新加载到对象数组中并序列化它:
private void SaveSettings()
{
this.Settings = new List<SavedSettings>();
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
if (!row.IsNewRow)
{
foreach (DataGridViewCell cell in row.Cells)
{
SavedSettings setting = new SavedSettings();
setting.BackColor = cell.Style.BackColor.ToArgb() == 0 ? Color.White : cell.Style.BackColor;
setting.ForeColor = cell.Style.ForeColor.ToArgb() == 0 ? Color.Black : cell.Style.ForeColor; ;
setting.Value = cell.Value;
this.Settings.Add(setting);
}
}
}
this.WriteXML();
}
private void WriteXML()
{
System.Xml.Serialization.XmlSerializer writer =
new System.Xml.Serialization.XmlSerializer(typeof(List<SavedSettings>));
System.IO.StreamWriter file = new System.IO.StreamWriter(@"SavedSettings.xml");
writer.Serialize(file, this.Settings);
file.Close();
}
注意这将允许某些属性持续存在。当然还有更好的方法,如果时间允许,我很想学习它们,但这个例子可以完成任务。至于额外的Excel要求,到目前为止我还没有尝试过。我想补充你的问题,以反映吸引Excel导向答案的要求。