在C#中保存并加载DataGridView内容和单元格样式 [英] Save and load DataGridView content and cell style in C#

查看:267
本文介绍了在C#中保存并加载DataGridView内容和单元格样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多列和行的DataGridView,用户可以右键单击一个单元格并从ContextMenuStrip中选择一个选项。选项为红色,蓝色,绿色等颜色,如果用户选择红色,则所选单元格将其BackColor设置为红色,用户也可以在该单元格中写入值。好吧,我的问题是,我找不到保存所有内容和样式的方法,所以如果用户重新打开for,dataGridView将有其最后的设置(包括单元格的BackColor和ForeColor)。 />


我试过这个来保存内容,它给了我错误,我不知道怎么打开它。



I have a DataGridView with many columns and rows, the user is able to right click in a cell and select an option from a ContextMenuStrip. The options are in Colors like Red, Blue, Green, etc, if the user selects, for example, Red, the selected cell sets its BackColor to Red, and the user is also able to write a value in that cell. Well, my problem is that, I cannot find a way to save all the content and style, so if the user re-opens the for, the dataGridView wil have its last settings (including the BackColor and ForeColor of the cells).

I tried this to save the content, it gave me error, and I don't know how to open it.

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);
       }
   }



如果有更好的方法来保存内容和样式,我们将不胜感激。先谢谢


If there is a better way to save the content and style, it is appreciated. Thanks in Advance

推荐答案

我会创建两个表:

grid_styles_header(id,grid_control_id,user_id)

grid_styles_cells(id,header_id,row_unique_identifier,column_name,color)



WHERE

来自单元格的header_id引用来自标题的id

row_unique_identifier是查询的主键,因此您可以知道要更新哪一行而不是使用索引。

column_name - datagrid列名或数据库列名,以便您知道哪个列会影响而不管列重新排序一些用户可能会尝试。



我不知道任何内置解决方案,但可能有第三方控件允许这样做。



如果这有帮助请花时间接受解决方案。谢谢。





注意:除了节点
I would create two tables:
grid_styles_header (id, grid_control_id, user_id)
grid_styles_cells (id, header_id, row_unique_identifier, column_name, color)

WHERE
header_id from cells references id from header
row_unique_identifier is your primary key for the query so you can know which row to update uniquely instead of using indexes.
column_name - datagrid column name or database column name so you know which column to affect regardless of column reordering some users might try.

I'm not aware of any built-in solution, but there may be third party controls that allow this.

If this helps please take time to accept the solution. Thank you.


Note: same saving logic goes for XML except you have node
<grid id="" user_id="">
<cell row_id="" column_name="" color="" />
<cell row_id="" column_name="" color="" />
<cell row_id="" column_name="" color="" />
</grid>





祝你好运



Good luck


如果你想写DataTable作为XML,您必须为DataTable命名。例如dt.TableName =XYZ和dt.WriteXML(...)。
If you want to write DataTable as XML you must give name to DataTable. e.g. dt.TableName = "XYZ" and dt.WriteXML(...).


这篇关于在C#中保存并加载DataGridView内容和单元格样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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