2012-08-22 2 views
3

데이터베이스의 하이브리드 "하드 코딩 된"문자열 및 데이터가 될 데이터를 표시해야합니다. 특히 모든 짝수 열에는 데이터베이스가 아닌 문자열 값이 들어 있으며 모든 홀수 열에는 데이터가 들어 있습니다.DataGridView에 세로 열을 번갈아 표시 할 수 있습니까?

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

이게 가능 : 첫 번째 두 개의 열이 같은 모양 (같은 패턴이 여러 번 반복) 그래야 열 (1)는, 예를 들어, 데이터베이스에서 (12)을 통해 값 하나를 포함 할 것인가?

지금 당장은 TableLayoutPanel을 사용하고 있습니다.하지만 그 코드는 Steely Dan의 레퍼런스가 아닌 약간의 프리 젤리입니다.

+0

네, 가능에서 모하메드 만수르의 코드에 따라이 작업을 얻었다. 지금까지 뭐 해봤 어? 내가 처음부터이 작업을 수행했다면 데이터베이스에 '하드 코드 된'값을 넣었습니다 ... 어떤 식 으로든 바인딩 된 값과 관련되지 않습니까? –

+0

데이터베이스 내용을 변경할 수있는 권한이 없습니다. 그러나 "I"는 "00:00", "2"는 "00:15", ... "96"은 23으로 표시됩니다. : 45 ",하지만 내 주요 도전 과제는 가로로 세로로 (가로로) 표시하는 대신 세로로 표시하는 방법입니다. 가능한 경우 DataGridView를 대신 사용하십시오. 데이터를 세로로 표시 할 수 있습니까? –

+1

질문 자체의 업데이트가 아닌 자신의 질문에 대한 답변으로 솔루션을 게시하십시오. – mclark1129

답변

0

드디어 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; 
} 
1

DataTable 개체를 만들고 DataGridView의 DataSource로 설정할 수 있습니다. 무엇이든 가능 -

관련 문제