将文本添加到DataGridView行标题
C#允许您将一个String添加到DataGridView中的RowHeader吗? 如果是这样,它是如何完成的?
我正在编写Windows窗体,以显示到目前为止的年度客户付款数据。
ColumnHeaders显示1月,2月,3月等…而不是有一个DateTime.Now.Year的空白列我想把它放在RowHeader中,使其从实际的支付数据中脱颖而出。
private void dtgworkingdays_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { this.FillRecordNo(); } private void FillRecordNo() { for (int i = 0; i < this.dtworkingdays.Rows.Count; i++) { this.dtgworkingdays.Rows[i].HeaderCell.Value = (i + 1).ToString(); } }
另请参阅在DataGridView的行标题中显示行号 。
datagridview1.Rows[0].HeaderCell.Value = "Your text";
有用。
您不必使用RowValidated事件,这只是我用于一个小测试应用程序的事件,以确保它有效,但这会将行(而不是列)标题文本设置为您指定的任何年份。
实际上,在CellFormatting事件中可能会更好。
private void dataGridView_RowValidated(object sender, DataGridViewCellEventArgs e) { DataGridView gridView = sender as DataGridView; if (null != gridView) { gridView.Rows[e.RowIndex].HeaderCell.Value = "2009"; } }
编辑 :这是我使用的整个TestForm,尽可能简单地演示解决方案。 确保您的RowHeadersWidth足够宽以显示文本。
#region using System.ComponentModel; using System.Windows.Forms; #endregion namespace DataGridViewTest { public class GridTest : Form { /// /// Required designer variable. /// private IContainer components; private DataGridView dataGridView1; private DataGridViewTextBoxColumn Month; public GridTest() { InitializeComponent(); } /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } private void dataGridView_RowValidated(object sender, DataGridViewCellEventArgs e) { DataGridView gridView = sender as DataGridView; if (null != gridView) { gridView.Rows[e.RowIndex].HeaderCell.Value = "2009"; } } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.Month = new System.Windows.Forms.DataGridViewTextBoxColumn(); ((System.ComponentModel.ISupportInitialize) (this.dataGridView1)).BeginInit(); this.SuspendLayout(); // // dataGridView1 // this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Month }); this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill; this.dataGridView1.Location = new System.Drawing.Point(0, 0); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.RowHeadersWidth = 100; this.dataGridView1.Size = new System.Drawing.Size(745, 532); this.dataGridView1.TabIndex = 0; this.dataGridView1.RowValidated += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView_RowValidated); // // Month // this.Month.HeaderText = "Month"; this.Month.Name = "Month"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(745, 532); this.Controls.Add(this.dataGridView1); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize) (this.dataGridView1)).EndInit(); this.ResumeLayout(false); } #endregion } }
这是因为你的第一列(Rows Header列)宽度! 增加它的宽度然后你可以看到它的价值! 你可以使用这个命令:
dgv1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
(注意:你必须先设置dgv1.RowHeadersVisible = true;
)
我遇到了同样的问题,但我注意到datagrid.visible
属性发生更改后,我的datagrid丢失了行的标题。
尝试使用Datagrid.visiblechanged
事件更新行的标头。
是的你可以
DataGridView1.Rows[0].HeaderCell.Value = "my text";
我有同样的问题。 无法获取标题列以使用我的数据绑定网格显示行标题数据(简单的行号)。 一旦我将代码移动到事件“DataBindingComplete”就可以了。
抱歉,这是额外的代码。 我想提供一个工作示例,但没有时间将其全部删除,所以只需剪切并粘贴我的一些应用程序并修复它以便为您运行。 干得好:
using System; using System.Collections.Generic; using System.Data; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace WindowsFormsApplication3 { public partial class Form1 : Form { private List pts = new List (); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { InsertPoint(10, 20); InsertPoint(12, 40); InsertPoint(16, 60); InsertPoint(20, 77); InsertPoint(92, 80); MakeGrid(); } public void InsertPoint(int parameterValue, int commandValue) { DataPoint pt = new DataPoint(); pt.XValue = commandValue; pt.YValues[0] = parameterValue; pts.Add(pt); } private void MakeGrid() { dgv1.SuspendLayout(); DataTable dt = new DataTable(); dt.Columns.Clear(); dt.Columns.Add("Parameter"); dt.Columns.Add("Command"); //*** Add Data to DataTable for (int i = 0; i <= pts.Count - 1; i++) { dt.Rows.Add(pts[i].XValue, pts[i].YValues[0]); } dgv1.DataSource = dt; //*** Formatting for the grid is performed in event dgv1_DataBindingComplete. //*** If its performed here, the changes appear to get wiped in the grid control. } private void dgv1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { DataGridViewCellStyle style = new DataGridViewCellStyle(); style.Alignment = DataGridViewContentAlignment.MiddleRight; //*** Add row number to each row foreach (DataGridViewRow row in dgv1.Rows) { row.HeaderCell.Value = (row.Index + 1).ToString(); row.HeaderCell.Style = style; row.Resizable = DataGridViewTriState.False; } dgv1.ClearSelection(); dgv1.CurrentCell = null; dgv1.ResumeLayout(); } } }
我认为它应该是:
dataGridView1.Columns[0].HeaderCell.Value = "my text";
foreach (DataGridViewRow row in datagrid.Rows) row.HeaderCell.Value = String.Format("{0}", row.Index + 1);
这是一个小小的“政变”
Public Class DataGridViewRHEx Inherits DataGridView Protected Overrides Function CreateRowsInstance() As System.Windows.Forms.DataGridViewRowCollection Dim dgvRowCollec As DataGridViewRowCollection = MyBase.CreateRowsInstance() AddHandler dgvRowCollec.CollectionChanged, AddressOf dvgRCChanged Return dgvRowCollec End Function Private Sub dvgRCChanged(sender As Object, e As System.ComponentModel.CollectionChangeEventArgs) If e.Action = System.ComponentModel.CollectionChangeAction.Add Then Dim dgvRow As DataGridViewRow = e.Element dgvRow.DefaultHeaderCellType = GetType(DataGridViewRowHeaderCellEx) End If End Sub End Class
Public Class DataGridViewRowHeaderCellEx Inherits DataGridViewRowHeaderCell Protected Overrides Sub Paint(graphics As System.Drawing.Graphics, clipBounds As System.Drawing.Rectangle, cellBounds As System.Drawing.Rectangle, rowIndex As Integer, dataGridViewElementState As System.Windows.Forms.DataGridViewElementStates, value As Object, formattedValue As Object, errorText As String, cellStyle As System.Windows.Forms.DataGridViewCellStyle, advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, paintParts As System.Windows.Forms.DataGridViewPaintParts) If Not Me.OwningRow.DataBoundItem Is Nothing Then If TypeOf Me.OwningRow.DataBoundItem Is DataRowView Then End If End If 'HERE YOU CAN USE DATAGRIDROW TAG TO PAINT STRING formattedValue = CStr(Me.DataGridView.Rows(rowIndex).Tag) MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts) End Sub End Class
是。 首先,挂钩添加事件列:
this.dataGridView1.ColumnAdded += new DataGridViewColumnEventHandler(dataGridView1_ColumnAdded);
然后,在您的事件处理程序中,只需附加您想要的文本:
private void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e) { e.Column.HeaderText += additionalHeaderText; }
确保选中“ Enable Column Recording
。