powered by simpleCommunicator - 2.0.52     © 2025 Programmizd 02
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / Datagridview перерисовка ячейки
4 сообщений из 4, страница 1 из 1
Datagridview перерисовка ячейки
    #39501141
Bujhm_C
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Есть код прорисовки ячейки Datagridview - вставка иконки.

Код: vbnet
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
 Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
Try
            'ВСТАВКА ИКОНКИ В ЯЧЕЙКУ
            If e.RowIndex >= 0 AndAlso e.ColumnIndex = DataGridView1.Columns(5).Index Then
                'e.Paint(e.ClipBounds, e.PaintParts);
                e.PaintBackground(e.ClipBounds, e.PaintParts)
                Dim p As PointF = e.CellBounds.Location
                p.X += ImageList1.ImageSize.Width
                p.Y += ImageList1.ImageSize.Height - 14

                Select Case DataGridView1.Rows(e.RowIndex).Cells(5).Value.ToString
                    Case "Завершена"
                        e.Graphics.DrawImage(ImageList1.Images(7), e.CellBounds.X, e.CellBounds.Y, 16, 16)
                    Case "Торги відмінено"
                        e.Graphics.DrawImage(ImageList1.Images(12), e.CellBounds.X, e.CellBounds.Y, 16, 16)
                    Case Else
                        e.Graphics.DrawImage(Nothing, e.CellBounds.X, e.CellBounds.Y, 16, 16)
                End Select

                Try
                    'ПЕРЕРИСОВКА ТЕКСТА В ЯЧЕЙКЕ
                    If e.Value IsNot Nothing Then
                        Dim strFormat As New StringFormat()
                        strFormat.Trimming = StringTrimming.EllipsisCharacter

                        If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True Then
                            e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.White, p)
                        Else
                            '  e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, New SolidBrush(Color.Black), p, StringFormat.GenericDefault)
                            '  e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, New SolidBrush(Color.Black), p, strFormat)

                            Dim sf As New StringFormat
                            With sf
                                .Trimming = StringTrimming.EllipsisCharacter
                            End With


                            e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, New SolidBrush(Color.Black), p, New StringFormat(sf))


                        End If
                    End If

                Catch ex As Exception

                End Try

                e.Handled = True
            End If

        Catch ex As Exception

        End Try
End Sub



А вот собственно вопрос. Как прописать троеточие в конце текста, если длина текста превышает длину ячейки? Все манипуляции с EllipsisCharacter не действуют. Надеюсь, что кто то поможет.
...
Рейтинг: 0 / 0
Datagridview перерисовка ячейки
    #39501162
Roman Mejtes
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
вы передаете в e.Graphics.DrawString не RectangeF, а PointF, по этому текст не обрезается, так как не понятно, какая передельная ширина текста.
...
Рейтинг: 0 / 0
Datagridview перерисовка ячейки
    #39501207
Bujhm_C
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Не помогло! Что делаю не так?

Код: vbnet
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
               Try
            'ВСТАВКА ИКОНКИ В ЯЧЕЙКУ
            If e.RowIndex >= 0 AndAlso e.ColumnIndex = DataGridView1.Columns(5).Index Then
                'e.Paint(e.ClipBounds, e.PaintParts);
                e.PaintBackground(e.ClipBounds, e.PaintParts)
                Dim p As PointF = e.CellBounds.Location
                p.X += ImageList1.ImageSize.Width
                p.Y += ImageList1.ImageSize.Height - 14

                Select Case DataGridView1.Rows(e.RowIndex).Cells(5).Value.ToString
                    Case "Завершена"
                        e.Graphics.DrawImage(ImageList1.Images(7), e.CellBounds.X, e.CellBounds.Y, 16, 16)
                    Case "Торги відмінено"
                        e.Graphics.DrawImage(ImageList1.Images(12), e.CellBounds.X, e.CellBounds.Y, 16, 16)
                    Case Else
                        e.Graphics.DrawImage(Nothing, e.CellBounds.X, e.CellBounds.Y, 16, 16)
                End Select

                Try
                    'ПЕРЕРИСОВКА ТЕКСТА В ЯЧЕЙКЕ
                    If e.Value IsNot Nothing Then
                        Dim strFormat As New StringFormat()
                        strFormat.Trimming = StringTrimming.EllipsisCharacter
                        Dim rec As New RectangleF(e.CellBounds.Location, New Size(0, 0))
                        rec.X += ImageList1.ImageSize.Width

                        If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True Then
                            e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.White, rec)
                        Else
                            '  e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, New SolidBrush(Color.Black), p, StringFormat.GenericDefault)
                            e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, New SolidBrush(Color.Black), rec, strFormat)

                        End If
                    End If

                Catch ex As Exception

                End Try

                e.Handled = True
            End If

        Catch ex As Exception

        End Try

    End Sub
...
Рейтинг: 0 / 0
Datagridview перерисовка ячейки
    #39501282
Bujhm_C
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Подмечено, задавая вопрос начинаешь разбираться и сам отвечаешь на свой же поставленный вопрос. Самопиар- так сказать!
Получилось, может кому и пригодится.
Всем спасибо за внимание.
Вот рабочий код:

Код: vbnet
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
 Try
            'ВСТАВКА ИКОНКИ В ЯЧЕЙКУ
            If e.RowIndex >= 0 AndAlso e.ColumnIndex = DataGridView1.Columns(5).Index Then
                e.PaintBackground(e.ClipBounds, e.PaintParts)

                Select Case DataGridView1.Rows(e.RowIndex).Cells(5).Value.ToString
                    Case "Завершена"
                        e.Graphics.DrawImage(ImageList1.Images(7), e.CellBounds.X, e.CellBounds.Y, 16, 16)
                    Case "Торги відмінено"
                        e.Graphics.DrawImage(ImageList1.Images(12), e.CellBounds.X, e.CellBounds.Y, 16, 16)
                    Case Else
                        e.Graphics.DrawImage(Nothing, e.CellBounds.X, e.CellBounds.Y, 16, 16)
                End Select

                Try
                    'ПЕРЕРИСОВКА ТЕКСТА В ЯЧЕЙКЕ
                    If e.Value IsNot Nothing Then
                        Using sf = New StringFormat()
                            Using sBrush = New SolidBrush(e.CellStyle.ForeColor)
                                sf.FormatFlags = StringFormatFlags.NoWrap
                                sf.Trimming = StringTrimming.EllipsisCharacter
                                Dim text As String = DirectCast(e.FormattedValue, String)
                                Dim rect = New RectangleF(e.CellBounds.X + 16, e.CellBounds.Y + 2, e.CellBounds.Width - 16, e.CellBounds.Height - 2)

                                If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True Then
                                    e.Graphics.DrawString(text, e.CellStyle.Font, Brushes.White, rect, sf)
                                Else
                                    e.Graphics.DrawString(text, Font, sBrush, rect, sf)
                                End If

                            End Using
                        End Using

                    End If
                Catch ex As Exception
                End Try
                e.Handled = True
            End If
        Catch ex As Exception

        End Try
...
Рейтинг: 0 / 0
4 сообщений из 4, страница 1 из 1
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / Datagridview перерисовка ячейки
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]