Click here to Skip to main content
16,004,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am reading the datas from excel and binding with datagridview.
In the datagridview, datas are displayed as in excel.
But the header of datagrid is displaying like F1,F2,F3..... above each datagrid columns.

Now i want to diplay the alphabets A,B,C,D as in excel in my datagrid view.
Also i want the row numbers 1,2,3 in my datagridview as we see in excel.

I want the row and column format of excel??
How can i do that with .net windows c#?
Can anyone help me in this??

Thanks in Advance!

Mohana
Posted

1 solution

You can freeze columns and rows in DataGridView (see MSND: columns[^], rows[^]).
You can fill the first frozen column with numbers, first row (header) with letters. Think about the letter numbering as if if were a special number system of 26 digits (see some methods here: here[^]).

Update: here you have a sample, assuming that dataGridView1 is your gridview.
C#
public static string ExcelColumnFromNumber(int column)
        {
            string columnString = "";
            decimal columnNumber = column;
            while (columnNumber > 0)
            {
                decimal currentLetterNumber = (columnNumber - 1) % 26;
                char currentLetter = (char)(currentLetterNumber + 65);
                columnString = currentLetter + columnString;
                columnNumber = (columnNumber - (currentLetterNumber + 1)) / 26;
            }
            return columnString;
        }

        private void prepareGrid(DataGridView grid, int rows, int cols)
        {
            grid.ColumnCount = cols;
            grid.RowCount = rows;
            grid.RowHeadersVisible = true;
            grid.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
            for (int i = 0; i < rows; i++)
            {
                grid.Rows[i].HeaderCell.Value = (i+1).ToString();
            }
            for (int i = 0; i < cols; i++)
            {
                grid.Columns[i].HeaderCell.Value = ExcelColumnFromNumber(i+1);
            }
        }


        private void Form1_Shown(object sender, EventArgs e)
        {
            prepareGrid(dataGridView1, 100, 100);
        }



Auto-growing the grid is an other topic.
 
Share this answer
 
v5
Comments
Mohanapriya Ranganathan 28-May-12 5:24am    
Thanks zoltan. can u give some coding example?
It will be very useful for me.
Zoltán Zörgő 28-May-12 7:15am    
Ok, but for which part?
Zoltán Zörgő 28-May-12 8:18am    
See updated answer

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