可以用交替的垂直列填充DataGridView吗? [英] Is it possible to populate a DataGridView with alternating vertical columns?

查看:146
本文介绍了可以用交替的垂直列填充DataGridView吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示混合硬编码字符串和数据库中的数据的数据。具体来说,每个偶数列都包含非数​​据库的字符串值,每个奇数列都包含数据。因此,列1例如将包含数据库中的值1到12,因此前两列看起来像这样(并且相同的模式重复几次):

I need to display data that will be hybrid "hard-coded" strings and data from a database. Specifically, every even-numbered column contains string values that are NOT from the database, and every odd-numbered column contains data. So column 1, for example, will contain values 1 through 12 from the database, so that the first two columns look something like this (and the same pattern repeats several times):

00:00    BoundVal1
00:15    BoundVal2
. . .
02:45    BoundVal12

这可能吗?

现在我正在使用一个TableLayoutPanel,但是它的编码有点小巧(不是Steely Dan参考)。

Right now I'm using a TableLayoutPanel for this, but the coding for that is a little pretzly (not a Steely Dan reference).

推荐答案

我终于得到了这个工作,根据Mohamed Mansour的代码 http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/ca89a857-6e17-44a7-8cdb-90d64a0a7bbe

I finally got this working, based on code from Mohamed Mansour at http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/ca89a857-6e17-44a7-8cdb-90d64a0a7bbe

int RowCount = 12; 
Dictionary<int, string> PlatypusPairs;

. . .

private void button3_Click(object sender, EventArgs e) {
    // Retrieve data as dictionary 
    PlatypusPairs = InterpSchedData.GetAvailableForPlatypusAndDate(oracleConnectionTestForm, 42, DateTime.Today.Date);
    int ColumnCount = 16;
    // Add the needed columns
    for (int i = 0; i < ColumnCount; i++) {
        string colName = string.Format("Column{0}", i + 1);
        dataGridView1.Columns.Add(colName, colName); 
    }

    for (int row = 0; row < RowCount; row++) {
        // Save each row as an array
        string[] currentRowContents = new string[ColumnCount];
        // Add each column to the currentColumn
        for (int col = 0; col < ColumnCount; col++) {
            currentRowContents[col] = GetValForCell(row, col);
        }
        // Add the row to the DGV
        dataGridView1.Rows.Add(currentRowContents);
    }
}

private string GetValForCell(int Row, int Col) {
    string retVal;

    if (Col % 2 == 0) {
        retVal = GetTimeStringForCell(Row, Col);
    } else {
        retVal = GetPlatypusStringForCell(Row, Col);
    }
    return retVal;
}

private string GetTimeStringForCell(int Row, int Col) {
    const int TIME_INCREMENT_STEP = 15;
    var dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
    dt = dt.AddMinutes(((Col * (RowCount / 2)) + Row) * TIME_INCREMENT_STEP);
    return dt.ToString("HH:mm");
}

private string GetPlatypusStringForCell(int Row, int Col) {
    int multiplicand = Col / 2;
    string val = string.Empty;
    int ValToSearchFor = (multiplicand * RowCount) + (Row + 1);
    if (PlatypusPairs.ContainsKey(ValToSearchFor)) {
        PlatypusPairs.TryGetValue(ValToSearchFor, out val);
        if (val.Equals(0)) {
            val = string.Empty;
        }
    }
    return val;
}

这篇关于可以用交替的垂直列填充DataGridView吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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