在datgridview中如何检查一个单元格,特别是coulmn [英] how to check a cell in particular coulmn in datgridview

查看:234
本文介绍了在datgridview中如何检查一个单元格,特别是coulmn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下方法在窗口格式的datagridview中显示一个组合框。我们如何传递参数,当它们控制特定列的任何单元格时,在datagridview的事件下。

  private void Show_Combobox(int iRowIndex,int iColumnIndex)
{
/ / DESCRIPTION:显示所选网格中的COMBO BOX。
//参数:iRowIndex - 网格的标识。
// iColumnIndex - 网格的列号。

int x = 0;
int y = 0;
int Width = 0;
int height = 0;

//获取活动细胞的含义,与其结合。
Rectangle rect = default(Rectangle);
rect = dataGridView1.GetCellDisplayRectangle(iColumnIndex,iRowIndex,false);
x = rect.X + dataGridView1.Left;
y = rect.Y + dataGridView1.Top;

Width = rect.Width;
height = rect.Height;

comboBox1.SetBounds(x,y,Width,height);
comboBox1.Visible = true;
comboBox1.Focus();
}

提前感谢

解决方案

您实际上可以使用 DataGridViewComboBoxCell



这里是一个关于如何做的例子:
为了做到这一点,你可以将控件投射到组合框的事件上,最适合你。


i已经选择了 EdittingControlShowing ,因为它在控件显示之后出现,即组合框已被点击,所以它会下降它的下拉方法:

  private void gridView1_EditingControlShowing(object sender,DataGridViewEditingControlShowingEventArgs e)
{
if(e.Control。 GetType()== typeof(DataGridViewComboBoxEditingControl))
{
((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
}
}

这样你就可以让用户进入他/她如果预设值不存在于 ComboBox 项目。为了做到这一点,您可以采取自己的价值以下方法使用 CellValidating 事件:

  if(gridView1.CurrentCell.IsInEditMode)
{
if(gridView1.CurrentCell.GetType()== typeof(DataGridViewComboBoxCell))
{
if(!((DataGridViewComboBoxColumn )gridView1.Columns [e.ColumnIndex])Items.Contains(e.FormattedValue))
{
((DataGridViewComboBoxColumn)gridView1.Columns [e.ColumnIndex])。Items.Add(e.FormattedValue );
}
}
}

这将添加输入到在这种情况下 ComboBox 单元格的项目。


i have following method to display a combobox in datagridview in window form .

how i pass argument in it, when their is control over any cell of particular column and under which event of datagridview .

private void Show_Combobox(int iRowIndex, int iColumnIndex)
{
    // DESCRIPTION: SHOW THE COMBO BOX IN THE SELECTED CELL OF THE GRID.
    // PARAMETERS: iRowIndex - THE ROW ID OF THE GRID.
    //             iColumnIndex - THE COLUMN ID OF THE GRID.

    int x = 0;
    int y = 0;
    int Width = 0;
    int height = 0;

    // GET THE ACTIVE CELL'S DIMENTIONS TO BIND THE COMBOBOX WITH IT.
    Rectangle rect = default(Rectangle);
    rect = dataGridView1.GetCellDisplayRectangle(iColumnIndex, iRowIndex, false);
    x = rect.X + dataGridView1.Left;
    y = rect.Y + dataGridView1.Top;

    Width = rect.Width;
    height = rect.Height;

    comboBox1.SetBounds(x, y, Width, height);
    comboBox1.Visible = true;
    comboBox1.Focus();
}

thanks in advance ....

解决方案

you can in fact use DataGridViewComboBoxCell for your purpose.

here is an example on how to : in order to do that, you can cast the control to a combobox on the event of the combobox that suits you best.
i have chosen EdittingControlShowing as it is raised after the control is shown i.e the combobox has been clicked so it would rase its drop down method:

private void gridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
    {
        ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
    }
}

this way you will let the user enter his/her own value if the preset value is not present in the ComboBox's Items.in order to do that you can take the following approach using the CellValidating Event:

if (gridView1.CurrentCell.IsInEditMode)
{
    if (gridView1.CurrentCell.GetType() == typeof(DataGridViewComboBoxCell))
    {
        if (!((DataGridViewComboBoxColumn)gridView1.Columns[e.ColumnIndex]).Items.Contains(e.FormattedValue))
        {
            ((DataGridViewComboBoxColumn)gridView1.Columns[e.ColumnIndex]).Items.Add(e.FormattedValue);
        }
    }
}

this will add the input to the items of the ComboBox cell in this case.

这篇关于在datgridview中如何检查一个单元格,特别是coulmn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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