none
对于Winform下DataGridView控件中的问题 RRS feed

  • 问题

  • 对于Winform下DataGridView控件中的问题,就是在单元格中自定义一个用户控件,该怎么做。还有个小问题就是此自定义控件初始化是需要一个必须的参数。这个该怎么弄。求大牛给点提示。
    2017年8月1日 8:49

答案

  • Hi TheYangtzeTheYellowRiver,

    感谢你在MSDN论坛发帖。

    根据你的描述,我做了下面的代码,你可以直接拷贝过去,然后测试一下。

    在使用代码前,你需要构造一个usercontrol 控件,如果构造 usercontrol, 你可以参考下面的文档。

    http://www.c-sharpcorner.com/UploadFile/jayendra/how-to-create-usercontrol-in-windows-form-application/

    下面就是在datagridview 中添加自定义控件。

    private void Form1_Load(object sender, EventArgs e)
            {
                this.dataGridView1.DataSource = CreateData();
                this.dataGridView1.Columns[0].Width = 130;
    
                for (int i = 0; i < dataGridView1.RowCount - 1; i++)
                {
                    UserControl1 userControl = new UserControl1("1432135");
                    this.dataGridView1.Controls.Add(userControl);
                    userControl.Location = this.dataGridView1.GetCellDisplayRectangle(0, i, true).Location;
                    userControl.Size = this.dataGridView1.GetCellDisplayRectangle(0, i, true).Size;
                }
    
            }
    
            private DataTable CreateData()
            {
                DataTable T_Gender = new DataTable();
                T_Gender.Columns.AddRange(new DataColumn[] {
                    new DataColumn("UserControl"),
                    new DataColumn("GenderID"),
                    new DataColumn("GenderName"),
                });
    
                T_Gender.Rows.Add(new object[] { "", "1", "Man" });
                T_Gender.Rows.Add(new object[] { "", "2", "Female" });
                T_Gender.Rows.Add(new object[] { "", "3", "Secret" });
                return T_Gender;
            }

    user control 的初始化函数,你在里面可以添加一个参数,用来传递你说的参数,我这边传递的是string 类型。

     public partial class UserControl1 : UserControl
        {
            public UserControl1(string str)
            {
                InitializeComponent();
            }
        }

    下面是我测试的截图。

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年8月2日 3:04
    版主

全部回复

  • Hi TheYangtzeTheYellowRiver,

    感谢你在MSDN论坛发帖。

    根据你的描述,我做了下面的代码,你可以直接拷贝过去,然后测试一下。

    在使用代码前,你需要构造一个usercontrol 控件,如果构造 usercontrol, 你可以参考下面的文档。

    http://www.c-sharpcorner.com/UploadFile/jayendra/how-to-create-usercontrol-in-windows-form-application/

    下面就是在datagridview 中添加自定义控件。

    private void Form1_Load(object sender, EventArgs e)
            {
                this.dataGridView1.DataSource = CreateData();
                this.dataGridView1.Columns[0].Width = 130;
    
                for (int i = 0; i < dataGridView1.RowCount - 1; i++)
                {
                    UserControl1 userControl = new UserControl1("1432135");
                    this.dataGridView1.Controls.Add(userControl);
                    userControl.Location = this.dataGridView1.GetCellDisplayRectangle(0, i, true).Location;
                    userControl.Size = this.dataGridView1.GetCellDisplayRectangle(0, i, true).Size;
                }
    
            }
    
            private DataTable CreateData()
            {
                DataTable T_Gender = new DataTable();
                T_Gender.Columns.AddRange(new DataColumn[] {
                    new DataColumn("UserControl"),
                    new DataColumn("GenderID"),
                    new DataColumn("GenderName"),
                });
    
                T_Gender.Rows.Add(new object[] { "", "1", "Man" });
                T_Gender.Rows.Add(new object[] { "", "2", "Female" });
                T_Gender.Rows.Add(new object[] { "", "3", "Secret" });
                return T_Gender;
            }

    user control 的初始化函数,你在里面可以添加一个参数,用来传递你说的参数,我这边传递的是string 类型。

     public partial class UserControl1 : UserControl
        {
            public UserControl1(string str)
            {
                InitializeComponent();
            }
        }

    下面是我测试的截图。

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年8月2日 3:04
    版主
  • 感谢你的回复,Thank you very much.
    2017年8月2日 9:59