1

当我在 DataGridView 中使用值(例如“1”)填充选定单元格时,而不是简单地显示“1”,它们会显示“1...”

为什么会这样,我怎样才能防止省略号点显示?

更新

这是请求的代码(如下)。它看起来像什么的尖叫声(暂停,出于某种原因,posterous 不接受它作为一个点凸起或一个松鸦钉)。

无论如何,DGV 看起来像这样:

00:00 | 1...
00:15 | 
00:30 | 1...
00:45 | 1...

...应该是什么时候:

00:00 | 1
00:15 | 
00:30 | 1
00:45 | 1

正如最后一行的注释中所指出的,在测试数据中,电话值只是“1”。将鼠标悬停在该值上会显示“1”的工具提示/提示(不是“1...”或类似的)。

private void CreateAndPopulateDGVPlatypusScheduleCells()
{
    // Add the needed columns
    if (dataGridViewPlatypusSchedule.Columns.Count == 0) {
        for (int i = 0; i < PLATYPUS_SCHEDULE_COL_COUNT; i++) {
            string colName = string.Format("Column{0}", i + 1);
            dataGridViewPlatypusSchedule.Columns.Add(colName, colName);
            dataGridViewPlatypusSchedule.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
            dataGridViewPlatypusSchedule.Columns[i].Resizable = DataGridViewTriState.False;

            DataGridViewCell cell = new DataGridViewTextBoxCell();
            if (i % 4 == 0) {
                cell.Style.BackColor = Color.Bisque;
                dataGridViewPlatypusSchedule.Columns[i].CellTemplate = cell;
                dataGridViewPlatypusSchedule.Columns[i].Width = 41;
                dataGridViewPlatypusSchedule.Columns[i].ReadOnly = true;
            } else {
                cell.Style.BackColor = Color.White;
                dataGridViewPlatypusSchedule.Columns[i].CellTemplate = cell;
                dataGridViewPlatypusSchedule.Columns[i].Width = 13;
                dataGridViewPlatypusSchedule.Columns[i].ReadOnly = false;
            }
        }
    }

    // Add the needed rows
    if (dataGridViewPlatypusSchedule.Rows.Count == 0)
    {
        for (int row = 0; row < PlatypusScheduleGridRowCount; row++) {
            // Save each row as an array
            string[] currentRowContents = new string[PLATYPUS_SCHEDULE_COL_COUNT];
            // Add each column to the currentColumn
            for (int col = 0; col < PLATYPUS_SCHEDULE_COL_COUNT; col++)
            {
                currentRowContents[col] = this.GetPlatypusScheduleTimeStringForCell(row, col);
            }
            // Add the row to the DGV
            dataGridViewPlatypusSchedule.Rows.Add(currentRowContents);
        }
    }

    int dow = this.GetDOWAsInt(ActiveDow);
    listQHduckBill = InterpSchedData.GetPlatypusScheduleForFunnyMammal(platypusId, dow);
    foreach (var duckBill in listQHduckBill)
    {
        int QHCell = duckBill.QH;
        string ph1 = duckBill.PH1;
        string ph2 = duckBill.PH2;
        string ph3 = duckBill.PH3;
            if (!(string.IsNullOrWhiteSpace(ph1)))
        {
            PopulatePlatypusScheduleCell(QHCell, 1, ph1);
        }
        if (!(string.IsNullOrWhiteSpace(ph2))) {
            PopulatePlatypusScheduleCell(QHCell, 2, ph2);
        }
        if (!(string.IsNullOrWhiteSpace(ph3))) {
            PopulatePlatypusScheduleCell(QHCell, 3, ph3);
        }
    }
}

private void PopulatePlatypusScheduleCell(int cellToPopulate, int offset, string phoneVal)
{
    int timeColumnBase = (cellToPopulate / PLATYPUS_SCHEDULE_COL_COUNT);
    string colName = this.GetColumnToPopulate(timeColumnBase, offset);
    int rowToPopulate = GetRowToPopulate(cellToPopulate);
    DataGridViewRow dgvr = dataGridViewPlatypusSchedule.Rows[rowToPopulate];
    dgvr.Cells[colName].Value = phoneVal; // <- This (phoneVal) is "1" but displays as "1..."
}
4

1 回答 1

0

我使用了 引用的 msdn 代码,但合并了 StringFormat 对象,并将花哨的深红色和蓝色替换为黑色(用于内容字体)和 blanchedalmond,以融入我的网格。我从 msdn 代码中更改的三件事已注明/注释:

private void dataGridViewLifeSchedule_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
    // 1. This is used to replace what is StringFormat.GenericDefault in the msdn code with strFormat
    StringFormat strFormat = new StringFormat();
    strFormat.Trimming = StringTrimming.None; 

    Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
    e.CellBounds.Y + 1, e.CellBounds.Width - 4,
    e.CellBounds.Height - 4);

    using (
        Brush gridBrush = new SolidBrush(this.dataGridViewLifeSchedule.GridColor),
        backColorBrush = new SolidBrush(e.CellStyle.BackColor)) {
        using (Pen gridLinePen = new Pen(gridBrush)) {
            // Erase the cell.
            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

            // Draw the grid lines (only the right and bottom lines; 
            // DataGridView takes care of the others).
            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                e.CellBounds.Bottom - 1);
            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                e.CellBounds.Top, e.CellBounds.Right - 1,
                e.CellBounds.Bottom);

            // Draw the inset highlight box.
            e.Graphics.DrawRectangle(Pens.BlanchedAlmond, newRect); // 2. It is Pens.Blue in the msdn code

            // Draw the text content of the cell, ignoring alignment. 
            if (e.Value != null) {
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                    Brushes.Black, e.CellBounds.X + 2, // 3. It is Brushes.Crimson in the msdn code
                    e.CellBounds.Y + 2, strFormat);
            }
            e.Handled = true;
        }
    }
}
于 2012-08-28T00:37:50.177 回答