[英].NET DataGridView: Remove “current row” black triangle
在 DataGridView 中,即使您將網格設置為只讀,在當前行顯示的行標題處也會有一個黑色三角形。
我想避免顯示它,我也想避免由三角形引起的那些單元格的大填充。 我猜填充是由三角形引起的,因為單元格的填充是 0。
有可能這樣做嗎? 如何?
謝謝!
編輯
這是創建行標題文本的方式:
for (int i = 0; i < 5; i++)
{
DataGridViewRow row = new DataGridViewRow();
row.HeaderCell.Value = headers[i];
dataGridView1.Rows.Add(row);
}
和headers
它是一個簡單的字符串數組。 ( string[]
)
如果您想保留行標題而不是隱藏它們,那么您可以使用單元格填充將三角形推到視線之外:
this.dataGridView1.RowHeadersDefaultCellStyle.Padding = new Padding(this.dataGridView1.RowHeadersWidth);
如果您使用行 header 文本並希望保持可見,您需要使用一些自定義繪畫 - 謝天謝地非常簡單。 在上面的代碼之后,只需附加到 RowPostPaint 事件,如下所示:
dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dataGridView1_RowPostPaint);
在 RowPostPaint 方法中:
void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { object o = dataGridView1.Rows[e.RowIndex].HeaderCell.Value; e.Graphics.DrawString( o?= null. o:ToString(), "". dataGridView1,Font. Brushes,Black. new PointF((float)e.RowBounds,Left + 2. (float)e.RowBounds;Top + 4)); }
正如 Dan Neely 指出的那樣,上面的Brushes.Black
的使用將覆蓋任何現有的更改,因此最好使用畫筆:
new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor)
將RowHeadersVisible
設置為false
。
一個非常簡單的解決方案是將行高設置為 16 像素或更小。 這將禁用 header 單元格行中的所有圖標。
dataGridView1.RowTemplate.Height = 16;
如果有人仍然想知道:
dataGridView1.RowHeadersWidth = 4; // the left row header size.
這將帶走三角形並縮小默認大小。
希望有幫助。
DataGridViewRowPostPaintEventArgs 包括這個特定的 PaintHeader 方法:
PaintHeader(DataGridViewPaintParts) - Paints the specified parts of the row header of the current row.
這是 DataGridViewPaintParts 枚舉: https://msdn.microsoft.com/en-us/library/ms159092%28v=vs.110%29.aspx
所以你要做的是在你的 datagridview 的 RowPostPaint 事件中,首先告訴它只繪制行的 header 的背景......就像這樣:
e.PaintHeader(DataGridViewPaintParts.Background)
然后告訴它繪制你想要的任何字符串。 這是我的例子:
Private Sub MyDGV_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles dgvData.RowPostPaint
Dim grid As DataGridView = DirectCast(sender, DataGridView)
e.PaintHeader(DataGridViewPaintParts.Background)
Dim rowIdx As String = (e.RowIndex + 1).ToString()
Dim rowFont As New System.Drawing.Font("Segoe UI", 9.0!, _
System.Drawing.FontStyle.Bold, _
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Dim centerFormat = New StringFormat()
centerFormat.Alignment = StringAlignment.Far
centerFormat.LineAlignment = StringAlignment.Near
Dim headerBounds As Rectangle = New Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height)
e.Graphics.DrawString(rowIdx, rowFont, SystemBrushes.ControlText, headerBounds, centerFormat)
End Sub
dataGridView1.CurrentCell = null;
將完全刪除黑色箭頭。
每次更新DataGridView
中的數據時都需要運行此行。
嘗試了其他答案,但我無法將相同的字體外觀與列字體匹配。 我在Microsoft 論壇上找到了解決方案
首先訂閱DataGridView.CellPainting
之類的;
dgv.CellPainting += Dgv_CellPainting;
然后打印任何你想要的文本(原始帖子有行索引);
private void Dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == -1 && e.RowIndex > -1)
{
object o = (sender as DataGridView).Rows[e.RowIndex].HeaderCell.Value;
e.PaintBackground(e.CellBounds, true);
using (SolidBrush br = new SolidBrush(Color.Black))
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(o.ToString(),
e.CellStyle.Font, br, e.CellBounds, sf);
}
e.Handled = true;
}
}
三角問題,很簡單,把
dgv_Products.Rows[xval].Selected = true;
dgv_Products.CurrentCell = dgv_Products.Rows[xval].Cells[0];
也就是將當前單元格屬性設置為當前選定行的單元格零。(測試為 dgv_Products.MultiSelect = false; 工作)
'Datagridview'.rowheadervisible=false
隱藏黑色箭頭行選擇器 ma bob
Datagridview 是您的數據網格的名稱,不帶引號 '' 所以.... 如果您的網格名為 Example 那么
Example.rowheadervisible=false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.