C#DataGridView Columnindex和Visual Display

发布于 2025-02-01 14:01:53 字数 260 浏览 1 评论 0原文

有一个DataGridView表,C#语言。 例如6列组成。有必要将第七个添加到桌子的末端,而是将其添加到第三名。 但是,因此,您将不得不重写很多代码,因为列索引会发生变化。 我对这个问题感兴趣:是否可以在第三位置上视觉显示列,并将其称为7

屏幕:

表列

There is a datagridview table, c# language.
Consisting, for example, of 6 columns. It is necessary to add the seventh, but not to the end of the table, but to the third place.
But because of this, you will have to rewrite a lot of code, because the column indexes will shift.
I'm interested in the question: is it possible to visually display a column in the third position, and refer to it as 7?*

*I understand that indexes start from zero and will be 2 and 6 respectively.

Screens:

Table columns

Visual display

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无妨# 2025-02-08 14:01:53

您的问题是:是否可以在第三位置上视觉显示列,并将其称为7?,答案是是YES

作为一个建议,最适合我的是完全不处理int索引,而是使用string datagridview.columns indexer 并使用datagridview.columns [“ columnName”]参考各个列。这解决了当索引从插入中变化时必须重写代码的问题(如您提到的),因为您可以用名称参考列。

有时您知道所有可能的列名可能是什么。在这种情况下,您可以通过将其抽象为枚举来使其更加健壮:

enum StdColumnName
{
     Apple0, 
     Orange1,
     Banana2,
}

示例 github

在mainform中覆盖此方法:

protected override void OnHandleCreated(EventArgs e)
{
    base.OnHandleCreated(e);
    foreach (string name in Enum.GetNames(typeof(StdColumnName)))
    {
        var col = new DataGridViewTextBoxColumn()
        {
            Name = name,
            HeaderText = name,
            AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill 
        };
        dataGridView.Columns.Add(col);
    }
}

要获得此方法:

“在此处输入图像说明”

然后单击按钮:

private void btnChangeOrder_Click(object sender, EventArgs e)
{
    var col = dataGridView.Columns[nameof(StdColumnName.Banana2)];
    col.DisplayIndex = 0;

    Debug.WriteLine(
        $"The Banana2.Index is { dataGridView.Columns[nameof(StdColumnName.Banana2)].Index}");
    Debug.WriteLine(
        $"but Banana2.DisplayIndex is { dataGridView.Columns[nameof(StdColumnName.Banana2)].DisplayIndex}");
}

要获取此:

现在col.indexcol.displayindex是两个不同的数字,但使用该名称仍然可行。

希望这给你一些想法。

Your question is: Is it possible to visually display a column in the third position, and refer to it as 7? and the answer is yes.

As a suggestion, what works best for me is to not deal with the int index at all, but to use the string indexer of Datagridview.Columns and use datagridview.Columns["columnName"] to refer to individual columns. This solves the problem of having to rewrite code when the index changes from an insert (as you mentioned) because you refer to columns by name.

Sometimes you know what all the possible column names might be. In this case, you can make this even more robust by abstracting them out to an enum:

enum StdColumnName
{
     Apple0, 
     Orange1,
     Banana2,
}

Example GitHub

Override this method in MainForm:

protected override void OnHandleCreated(EventArgs e)
{
    base.OnHandleCreated(e);
    foreach (string name in Enum.GetNames(typeof(StdColumnName)))
    {
        var col = new DataGridViewTextBoxColumn()
        {
            Name = name,
            HeaderText = name,
            AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill 
        };
        dataGridView.Columns.Add(col);
    }
}

to get this:

enter image description here

then click the button:

private void btnChangeOrder_Click(object sender, EventArgs e)
{
    var col = dataGridView.Columns[nameof(StdColumnName.Banana2)];
    col.DisplayIndex = 0;

    Debug.WriteLine(
        
quot;The Banana2.Index is { dataGridView.Columns[nameof(StdColumnName.Banana2)].Index}");
    Debug.WriteLine(
        
quot;but Banana2.DisplayIndex is { dataGridView.Columns[nameof(StdColumnName.Banana2)].DisplayIndex}");
}

to get this:

enter image description here

Now col.Index and col.DisplayIndex are two different numbers but using the name still works.

Hope this give you a few ideas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文