滚动时列DisplayIndex不正确

时间:2012-10-15 14:25:22

标签: c# winforms datagridview scroll

我正在使用DataGridView,并遇到一个问题,根据DisplayIndex,列有时不会以正确的顺序显示。我在另一个网站http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/35c1ed09-f4a4-4b3d-bd46-35a11cb8a9bc上发现了同样的问题。

问题似乎是当列未显示在网格的初始显示上时,由于向右移动并需要滚动才能看到它。当列数足够少以至于无需滚动即可看到所有列,则顺序正确。如果添加了一列以便其中一列需要滚动才能看到它,那么根据DisplayIndex,右边的列将不是最后一列,它将是一个应该在前面显示的列。

虽然我可以尝试更改窗口中的逻辑以与上面链接中的建议类似地工作,但我不认为基本问题只是被掩盖并且需要解决方法。有没有其他人有任何关于如何以解决问题本身而不是解决问题的方式解决这个问题的建议?

以下是我目前正在进行排序的代码片段:

// Fill the data grid view with data from the database.
dataGridView1.DataSource = null;
DataTable dt = // Logic to get data from DB here
dataGridView1.DataSource = dt;

// Get the column display data.  This can change per user.
DataTable columnDisplay = // Logic to get data from DB here

bool foundMatch;

foreach (DataGridViewColumn column in dataGridView1.Columns)
{
    foundMatch = false;
    foreach (DataRow row in columnDisplay.Rows)
    {
        if (row["column_name"].ToString().Equals(column.Name, StringComparison.CurrentCultureIgnoreCase))
        {
            foundMatch = true;
            if (row["show_in_window"].ToString().Equals("y"))
            {
                // Set the column placement and display name.
                dataGridView1.Columns[column.Name].DisplayIndex = Convert.ToInt32(row["column_sequence"].ToString());
                dataGridView1.Columns[column.Name].HeaderText = row["display_name"].ToString();
                dataGridView1.Columns[column.Name].Width = 105;
            }
            else
            {
                // Hide the column.
                dataGridView1.Columns[column.Name].Visible = false;
            }
            break;
        }
    }

    // If the column is not in the display table, then it should not be displayed at all.
    if (!foundMatch)
        column.Visible = false;
}

1 个答案:

答案 0 :(得分:0)

好吧,既然没有人回复,我会给出更新 - 我没有花时间解决这个问题。它被认为是一个足够低的优先级,如果有的话,我可能几个月都没有。

自原始帖子以来我发现的一条有趣的信息是,当修改列的显示索引号时,其余列会自动更改以考虑它。当我阅读msdn文档时,这对我来说并不明显,所以对于遇到这个问题的其他人来说,这可能会有所帮助。

相关问题