-->

是否有可能来填充交替垂直列一个DataGridView?(Is it possible to pop

2019-06-27 05:57发布

我需要显示,这将是混合动力“硬编码”从数据库字符串和数据。 具体而言,每一偶数列包含从数据库中没有字符串值,和每一个奇数列包含数据。 所以第1列,例如,将包含值1到12从数据库中,从而使前两列看起来像这样(和相同的模式重复几次):

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

这可能吗?

现在我用这个一个TableLayoutPanel,但该编码是有点pretzly(不是钢制的丹参考)。

Answer 1:

你可以只建立一个DataTable对象,并将其设置为DataGridView中的数据源。



Answer 2:

我终于得到这个工作的基础上,由穆罕默德·曼苏尔代码在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;
}


文章来源: Is it possible to populate a DataGridView with alternating vertical columns?