日期:2014-05-17 浏览次数:20908 次
public class MyDataGridView : DataGridView
{
public MyDataGridView(): base()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.Columns.Count > 0)
{
DataTable myTable = new DataTable();
if (this.DataSource != null)
{
myTable = (DataTable)this.DataSource;
}
else
{
//问题就出在这里,如果没有此循环,绑定的数据集为空,却没有列信息,不便于后期数据处理;如果加上此循环,在别的程序调用设计控件时,添加列会在*.Designer.cs文件中自动增加对应的列.
foreach (DataGridViewColumn myCol in this.Columns)
{
myTable.Columns.Add(myCol.Name, typeof(System.String));
}
}
//循环添加行
if (this.DisplayedRowCount(false) == this.Rows.Count)
{
while (this.DisplayedRowCount(false) == this.Rows.Count)
{
if (this.Rows.Count == 0)
{
myTable.Rows.Add(myTable.NewRow());
this.DataSource = myTable;