在Windows C#的每个datagrid列上方显示字母 [英] Displaying alphabets above each datagrid columns in windows c#

查看:96
本文介绍了在Windows C#的每个datagrid列上方显示字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从excel中读取数据并与datagridview绑定.
在datagridview中,数据显示为与excel中相同.
但是datagrid的标题在每个datagrid列上方显示为F1,F2,F3....

现在,我想在数据网格视图中像在excel中那样显示字母A,B,C,D.
我也想要在datagridview中的行号1,2,3,就像我们在excel中看到的一样.

我想要Excel的行和列格式?
如何使用.net Windows C#做到这一点?
有人可以帮我吗?

在此先感谢!

Mohana

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

推荐答案

您可以冻结DataGridView中的列和行(请参阅MSND: ^ ],行[ 此处 [
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.
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.


这篇关于在Windows C#的每个datagrid列上方显示字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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