Bai*_*ilz 23 .net c# datagridview winforms
C#允许您将一个String添加到DataGridView中的RowHeader吗?如果是这样,它是如何完成的?
我正在写一个Windows表单来显示到目前为止的年度客户付款数据.
ColumnHeaders显示1月,2月,3月等...而不是有一个DateTime.Now.Year的空白列我想把它放在RowHeader中,使其从实际的支付数据中脱颖而出.
小智 24
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();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 18
datagridview1.Rows[0].HeaderCell.Value = "Your text";
Run Code Online (Sandbox Code Playgroud)
有用.
您不必使用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";
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是我使用的整个TestForm,尽可能简单地演示解决方案.确保您的RowHeadersWidth足够宽以显示文本.
#region
using System.ComponentModel;
using System.Windows.Forms;
#endregion
namespace DataGridViewTest
{
public class GridTest : Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private IContainer components;
private DataGridView dataGridView1;
private DataGridViewTextBoxColumn Month;
public GridTest()
{
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
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
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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
}
}
Run Code Online (Sandbox Code Playgroud)
这是因为你的第一列(Rows Header列)宽度!增加它的宽度然后你可以看到它的价值!你可以使用这个命令:
dgv1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
Run Code Online (Sandbox Code Playgroud)
(注意:你必须先设定dgv1.RowHeadersVisible = true;)
BFr*_*ree -2
是的。首先,挂钩添加的列事件:
this.dataGridView1.ColumnAdded += new DataGridViewColumnEventHandler(dataGridView1_ColumnAdded);
Run Code Online (Sandbox Code Playgroud)
然后,在您的事件处理程序中,只需附加您想要的文本:
private void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
e.Column.HeaderText += additionalHeaderText;
}
Run Code Online (Sandbox Code Playgroud)