简体   繁体   English

网格视图在C#中的应用

[英]Application of grid view in c#

There are some Forms and some Custom User controls (custom drop down window containing dataGridView ) in my project.in a Form(Purchase order) have a dataGridView control, when clicking a cell this custom dropdown window will appear, when selecting a row in that drop down window or by clicking ENTER key these values (ie, cell values in a row of gridview) sholud moves to previous grid view of FORM. 我的项目中有一些Forms和一些Custom User控件(包含dataGridView的自定义下拉窗口)。在Form(购买顺序)中有一个dataGridView控件,当单击一个单元格时,在该单元格中选择一行时,将显示该自定义下拉窗口下拉窗口或单击ENTER键,这些值(即,网格视图行中的单元格值)将移至FORM的先前网格视图。 I tried this, can't get expected output...any one helpe... 我尝试了这个,无法获得预期的输出...任何人的帮助...

portion of code in custom user control:- 自定义用户控件中的代码部分:-

#region Declarations
    public string grdItmName;
    public string grdItmDisrption;
    public string grdItmCata;
    public string grdItmPrice;
    #endregion

    #region prop
    public string gridfield1
    {
        get
        {
            return grdItmName;
        }
        set { grdItmName = value; }
    }

    public string gridfield2
    {
        get
        {
            return grdItmDisrption;
        }
        set { grdItmDisrption = value; }
    }
    public string gridfield3
    {
        get
        {
            return grdItmCata;
        }
        set { grdItmCata = value; }
    }
    public string gridfield4
    {
        get
        {
            return grdItmPrice;
        }
        set { grdItmPrice = value; }
    }
#endregion

private void gridItemlist_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode.Equals(Keys.Enter))
        {

            try
            {
                grdItmName = gridItemlist.SelectedRows[0].Cells[0].Value.ToString();
                grdItmDisrption = gridItemlist.SelectedRows[0].Cells[1].Value.ToString();
                grdItmCata = gridItemlist.SelectedRows[0].Cells[2].Value.ToString();
                grdItmPrice = gridItemlist.SelectedRows[0].Cells[3].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


}

code snippets of FORM grid view:-- FORM网格视图的代码段:-

private void gridPrchaseOrder_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)

{


customDropDown.Show();
customDropDown.BringToFront();`// display in front of other controls`
customDropDown.Select();
customDropDown.Location = gridPrchaseOrder.PointToScreen(gridPrchaseOrder.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location);




        try
        {   
          int n = gridPrchaseOrder.Rows.Add();
          gridPrchaseOrder.Rows[n].Cells[0].Value = customDropDown.gridfield1;
            gridPrchaseOrder.Rows[n].Cells[1].Value = customDropDown.gridfield2;
            gridPrchaseOrder.Rows[n].Cells[2].Value = customDropDown.gridfield3;
            gridPrchaseOrder.Rows[n].Cells[3].Value = customDropDown.gridfield4;
            customDropDown.Focus();
        }
        catch (Exception ex) { MessageBox.Show(ex.Message); }

    }    

One observation, in the user/custom control Remove the above shown code and declare the properties with names as below. 用户/自定义控件中的一项观察结果删除上面显示的代码,并使用以下名称声明属性。 You don't require gridfield1, gridfield2, gridfield3 and gridfield4. 您不需要gridfield1,gridfield2,gridfield3和gridfield4。 Its not a good practice to have such variable names. 拥有这样的变量名不是一个好习惯。

public string grdItmName { get; set; }
public string grdItmDisrption { get; set; }
public string grdItmCata { get; set; }
public string grdItmPrice { get; set; }

Now in the form code 现在在表单代码中

        try
        {   
          int n = gridPrchaseOrder.Rows.Add();
          gridPrchaseOrder.Rows[n].Cells[0].Value = customDropDown.grdItmName;
            gridPrchaseOrder.Rows[n].Cells[1].Value = customDropDown.grdItmDisrption;
            gridPrchaseOrder.Rows[n].Cells[2].Value = customDropDown.grdItmCata;
            gridPrchaseOrder.Rows[n].Cells[3].Value = customDropDown.grdItmPrice;
            customDropDown.Focus();
        }
        catch (Exception ex) { MessageBox.Show(ex.Message); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM