如何在动态更改列的宽度后保存数据网格视图中列的宽度 [英] How to save width of a column in a datagridview after changing the width of a column dynamically

查看:68
本文介绍了如何在动态更改列的宽度后保存数据网格视图中列的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在datagridview中保存列的宽度。我已经编写了这段代码,但是没有得到正确的输出。



当我们关闭表单时,datagridview列的宽度将被保存。



每当我们反复调试时,它会在datagridview中显示列宽为已保存列宽







我应该在哪里写这些代码。



I want to save width of a column in a datagridview. I have written this code but I didn't get the correct output.

when we close a form the datagridview columns width will be saved.

Whenever we debug again and again it will show that column width as Saved column width

in the datagridview.

For that where should I write these code.

DataTable dt = new DataTable(DG.Name);
            dt.Columns.Add("name");
            dt.Columns.Add("width");
            dt.Columns.Add("index");
            for (int i = 0; i < DG.Columns.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["name"] = DG.Columns[i].Name;
                dr["width"] = DG.Columns[i].Width;
                dr["index"] = DG.Columns[i].DisplayIndex;
                string name = "";
                int width, index;
                name = dr["name"].ToString();
                width = Convert.ToInt32(dr["width"].ToString());
                index = Convert.ToInt32(dr["width"].ToString());
                Details_Grid.Columns[i].Width = width;
            }

请您建议我正确的代码

推荐答案

您正确获取列宽,将它们放入新的数据行在一个新的数据表,但你不是在会话之间保持信息(每次你开始调试)。



你需要做的是PERSIST数据,如果你赢了不使用数据库,然后您需要将其写入本地用户硬盘驱动器。然后当会话开始时读取该信息并格式化列宽。我不会使用索引,因为用户可能能够重新排序列。



这是文章 [ ^ ]我存储和读取表单状态。
You are getting the column widths correctly, placing them in a new datarow in a new datatable but you are not persisting the information between sessions(every time you start debug).

What you need to do is PERSIST the data, if you won't use a database then you need to write it to the local users hard drive. Then when the session starts READ that information and format the column widths. I would not use the index as the user may be able to reorder the columns.

Here is an article [^]where I store and read the forms states.


这篇关于如何在动态更改列的宽度后保存数据网格视图中列的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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