Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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;
            }
Would u please suggest me correct code
Posted
Updated 12-Oct-14 19:13pm
v3
Comments
ashok rathod 13-Oct-14 0:17am    
what is not working ?
[no name] 13-Oct-14 0:18am    
have you tried debugging? I think you are missing a code for adding that particular row to that data table.
dt.rows.add(dr). After do this, try to loop through again.
Sergey Alexandrovich Kryukov 13-Oct-14 0:36am    
Not clear. Save where and why?
—SA
TarunKumarSusarapu 13-Oct-14 1:14am    
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.
Sinisa Hajnal 13-Oct-14 2:19am    
You didn't answer: Save WHERE? There is no persistent storage (database?) in your code. There is an event ColumnWidthChanged or some such.

1 solution

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.
 
Share this answer
 
Comments
TarunKumarSusarapu 13-Oct-14 4:46am    
Would u please tell me how to use local users database
Mycroft Holmes 13-Oct-14 4:55am    
What database are you using for the local storage (I don't think you know). I would use an XML file, read the article it has example and code.
TarunKumarSusarapu 13-Oct-14 5:20am    
Here Have u taken empty xml file or adding any columns
TarunKumarSusarapu 13-Oct-14 5:23am    
Would u please explain breifly what should I do

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900