3

ハイブリッドの「ハードコードされた」文字列とデータベースからのデータになるデータを表示する必要があります。具体的には、すべての偶数列にはデータベースからのものではない文字列値が含まれ、すべての奇数列にはデータが含まれます。たとえば、列1にはデータベースの値1から12が含まれるため、最初の2つの列は次のようになります(同じパターンが数回繰り返されます)。

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

これは可能ですか?

現在、これにはTableLayoutPanelを使用していますが、そのコーディングは少しきれいです(Steely Danリファレンスではありません)。

4

2 に答える 2

1

DataTableオブジェクトを作成し、それをDataGridViewのデータソースとして設定するだけです。

于 2012-10-25T11:16:00.090 に答える
0

http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/ca89a857-6e17-44a7-8cdb-90d64a0a7bbeのMohamedMansourのコードに基づいて、ようやくこれが機能するようになりました。

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;
}
于 2012-10-25T19:23:48.740 に答える