c #datagridview行宽在滚动时自动增加

时间:2015-11-30 05:58:48

标签: c# winforms datagridview datagridviewcolumn

我的Datagridview包含DateTimeColumn,它在滚动时与另一个下一行列重叠。我已将DatagridView属性设置为

AutoSizeColumnsMode:填充

AutoSizeRowsMo​​de:AllCellsExceptheaders

AllowUserstoAddRows:True

但它告诉我以下结果。任何帮助都会很棒

enter image description here

我的代码

  private void dataGridView4_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            if (e.RowIndex != -1)
            {
                if (e.ColumnIndex == 1 || e.ColumnIndex == 2)
                {

                    oDateTimePicker = new DateTimePicker();


                    dataGridView4.Controls.Add(oDateTimePicker);


                    oDateTimePicker.Format = DateTimePickerFormat.Short;


                    Rectangle oRectangle = dataGridView4.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);


                    oDateTimePicker.Size = new Size(oRectangle.Width, oRectangle.Height);


                    oDateTimePicker.Location = new Point(oRectangle.X, oRectangle.Y);
                    oDateTimePicker.TextChanged += new EventHandler(dateTimePicker_OnTextChange);
                    oDateTimePicker.Visible = true;
                }
            }
        }
        catch (Exception E)
        {
            MessageBox.Show(E.ToString());
        }
    }



    private void dateTimePicker_OnTextChange(object sender, EventArgs e)
    {

        dataGridView4.CurrentCell.Value = oDateTimePicker.Text.ToString();
    }

1 个答案:

答案 0 :(得分:1)

似乎不是使用真实的DataGridViewColumn而是将DateTimePicker上的某些DataGridView对齐。

您可以创建自定义DateTimePicker,而不是在DataGridView上方对齐某些DataGridViewColumn控件。

这是一个实现CalendarColumn的好MSDN Example