删除与datagridview的单元格内容重叠的序列号? [英] Remove serial number which are overlapping over the cell contents of datagridview sequentially?

查看:124
本文介绍了删除与datagridview的单元格内容重叠的序列号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除DataGridview单元格上重叠的数字。我该怎么办?

ScreenShot在这里:

[截图]





  private   void  SearchForm_Load( object  sender,EventArgs e)
{
DataTable cmbTable = new DataTable();
DataGridViewTextBoxColumn dataGridViewTextBoxColumn = default (DataGridViewTextBoxColumn);
int index = 0 ;
int totalWidth = 0 ;

this .WindowState = FormWindowState.Normal;

cmbTable.Columns.Add( RecCode);
cmbTable.Columns.Add( TheName);

// 设置datagrid列和宽度等
< span class =code-keyword> for (index = 0 ; index < = this .displayColumns.Length - 1 ; index ++)
{
dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
dataGridViewTextBoxColumn.DataPropertyName = this .displayColumns [index];
dataGridViewTextBoxColumn.HeaderText = this .displayHeaders [index];
dataGridViewTextBoxColumn.Width = this .displayWidths [index];
this .DgSearch.Columns.Add(dataGridViewTextBoxColumn);

totalWidth + = this .displayWidths [index];

if this .DgSearch.DataSource DataTable)
{
this .DgSearch.DataSource =((DataTable)DgSearch.DataSource).DefaultView;
}

if (index == 0
{
this .Text = 搜索:;
}

cmbTable.Rows.Add( new object [ ] { this .displayColumns [index], this .displayHeaders [index]});
}

.columnValue = string .Empty;
.Width = totalWidth + 25 ;

// 组合框中的列标题,用于选择搜索列名称
this .CmbColumnName.DisplayMember = TheName ;
this .CmbColumnName.ValueMember = RecCode ;
this .CmbColumnName.DataSource = cmbTable;
this .CmbColumnName.SelectedIndex = 0 ;
}





我的尝试:



检查datagridview的属性

这是下面的Paint方法

  protected  覆盖  void  OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
{ // 此方法会覆盖DataGridView的RowPostPaint事件
// 以便自动在行标题单元格上绘制数字
// 并自动调整包含
// 行标题单元格,以便它可以容纳新行
// 数字,

// 在'strRowNumber'中存储行号的字符串表示
string strRowNumber =(e.RowIndex + 1 )。ToString();

// 如果需要改进,则在字符串前加零
// 外观。例如,如果网格中有十行,
// 第七行将是编号为07而不是7。同样,如果
// 网格中有100行,第7行将被编号as007。
while (strRowNumber.Length < this .RowCount.ToString()。Length)strRowNumber = 0 + strRowNumber;

// 使用 $ b确定行号字符串的显示大小$ b // DataGridView的当前字体。
SizeF size = e.Graphics.MeasureString( strRowNumber, this .Font);

// 调整包含行标题单元格的列的宽度
// 如有必要
if this .RowHeadersWidth < int )(size.Width + 20 )) this .RowHeadersWidth =( int )(size.Width + 20 );

// 此画笔将用于在
// 使用系统当前ControlText颜色的行标题单元格
Brush b = SystemBrushes.ControlText;

// 使用在当前行标题单元格上绘制行号字符串
// 上面定义的画笔和DataGridView的默认字体
e.Graphics .DrawString(strRowNumber, this .Font,b,e.RowBounds.Location.X + 15 ,e .RowBounds.Location.Y +((e.RowBounds.Height - size.Height)/ 2 ));

// 调用基础对象的OnRowPostPaint方法
.OnRowPostPaint(E);
}

解决方案

首先使用调试器确切地找出你放在那里的值 - 你的截图显示了一列,所以猜测你有一个宽度为零的秒。

同时检查你的DGV事件处理程序:如果你正在处理任何Cell或Row绘画事件以提供本地绘图,那可能造成这种影响。


我认为你应该是评论呼叫基础绘画方法:

 // base.OnRowPostPaint 


I want to remove the numbers overlapping on the cell of DataGridview. What should I do??
ScreenShot is here:
[Screenshot]


private void SearchForm_Load(object sender, EventArgs e)
        {
            DataTable cmbTable = new DataTable();
            DataGridViewTextBoxColumn dataGridViewTextBoxColumn = default(DataGridViewTextBoxColumn);
            int index = 0;
            int totalWidth = 0;

            this.WindowState = FormWindowState.Normal;

            cmbTable.Columns.Add("RecCode");
            cmbTable.Columns.Add("TheName");

            // set the datagrid columns and width etc
            for (index = 0; index <= this.displayColumns.Length - 1; index++)
            {
                dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
                dataGridViewTextBoxColumn.DataPropertyName = this.displayColumns[index];
                dataGridViewTextBoxColumn.HeaderText = this.displayHeaders[index];
                dataGridViewTextBoxColumn.Width = this.displayWidths[index];
                this.DgSearch.Columns.Add(dataGridViewTextBoxColumn);

                totalWidth += this.displayWidths[index];

                if (this.DgSearch.DataSource is DataTable)
                {
                    this.DgSearch.DataSource = ((DataTable)DgSearch.DataSource).DefaultView;
                }

                if (index == 0)
                {
                    this.Text = "Search: ";
                }

                cmbTable.Rows.Add(new object[] { this.displayColumns[index], this.displayHeaders[index] });
            }

            this.columnValue = string.Empty;
            this.Width = totalWidth + 25;

            // Column Headers in combo box to select the search column name
            this.CmbColumnName.DisplayMember = "TheName";
            this.CmbColumnName.ValueMember = "RecCode";
            this.CmbColumnName.DataSource = cmbTable;
            this.CmbColumnName.SelectedIndex = 0;
        }



What I have tried:

Checking the Properties of datagridview
This is the Paint Method below

protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        { //this method overrides the DataGridView's RowPostPaint event 
            //in order to automatically draw numbers on the row header cells
            //and to automatically adjust the width of the column containing
            //the row header cells so that it can accommodate the new row
            //numbers,

            //store a string representation of the row number in 'strRowNumber'
            string strRowNumber = (e.RowIndex + 1).ToString();

            //prepend leading zeros to the string if necessary to improve
            //appearance. For example, if there are ten rows in the grid,
            //row seven will be numbered as "07" instead of "7". Similarly, if 
            //there are 100 rows in the grid, row seven will be numbered as "007".
            while (strRowNumber.Length < this.RowCount.ToString().Length) strRowNumber = "0" + strRowNumber;

            //determine the display size of the row number string using
            //the DataGridView's current font.
            SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);

            //adjust the width of the column that contains the row header cells 
            //if necessary
            if (this.RowHeadersWidth < (int)(size.Width + 20)) this.RowHeadersWidth = (int)(size.Width + 20);

            //this brush will be used to draw the row number string on the
            //row header cell using the system's current ControlText color
            Brush b = SystemBrushes.ControlText;

            //draw the row number string on the current row header cell using
            //the brush defined above and the DataGridView's default font
            e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));

            //call the base object's OnRowPostPaint method
            base.OnRowPostPaint(e);
        }

解决方案

Start by using the debugger to find out exactly what values you are putting in there - your screenshot shows just one column, so at a guess you have a second with a zero width.
Also check your DGV event handlers: if you are processing any of the Cell or Row painting events to provide local drawing, that could be causing the effect.


I think you should be comment call base painting method:

//base.OnRowPostPaint


这篇关于删除与datagridview的单元格内容重叠的序列号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆