How to add Contols Dynamically in DataGridView Row in Windows Form Application?

后端 未结 1 999
余生分开走
余生分开走 2021-01-23 19:34

I have a DataGridView with Different Control Type in Each row now i want to create control in each row according to that Control Type Is it Possible? if Yes then How ?

相关标签:
1条回答
  • 2021-01-23 20:34

    You can add control on the OnRowDatabound of your grid, then you can a controls to your row

    To add a control to a cell use

    void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow row = e.Row as GridViewRow;
        if (row != null)
        {
            MyObject myObject = new MyObject();
            row.Cells[0].Controls.Add(myObject);
        }
    }
    

    To add a control to the entire row use

    void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow row = e.Row as GridViewRow;
        if (row != null)
        {
            MyObject myObject = new MyObject();
            row.Controls.Add(myObject);
        }
    }
    

    Have a look here for the windows forms Grid

    You can create your own column class by inheriting the DataGridViewColumn class or any of its derived classes to provide custom appearance, behavior, or hosted controls. For more information, see How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance

    http://msdn.microsoft.com/en-us/library/bxt3k60s%28v=vs.80%29.aspx

    How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance
    http://msdn.microsoft.com/en-us/library/7fb61s43%28v=vs.80%29.aspx

    0 讨论(0)
提交回复
热议问题