如何修复隐藏的DataGridView对象

时间:2019-08-12 09:48:21

标签: c# winforms

我将datagridview设置为对象,并在Textbox更改事件中以Windows窗体显示。当打开表单并开始向文本框发送文本时,将显示datagridview,但是当文本为空或null时,datagridview仍然可见。如何完成Windows窗体中不可见的datagridview?

这是针对C#的。我试图在if子句中放置datagridview,但是它没有用。

这是我的代码:

public class CreateDataGridView
    {
        public DataGridView clientsDgv = new DataGridView();

        public CreateDataGridView()
        {
            clientsDgv.ReadOnly = true;
            clientsDgv.Name = "clientsDgv";
            clientsDgv.AllowUserToAddRows = false;
            clientsDgv.AllowUserToDeleteRows = false;
            clientsDgv.AllowUserToResizeRows = false;  
        }

        public DataGridView Createdgv()
        {
            return clientsDgv;
        }

这是我在Windows窗体中的代码。

//Get datagridview in windows form textbox change event



private void TxtID_TextChanged(object sender, EventArgs e)
        {

            switch (this.cmbSelectAction.SelectedIndex)
            {
                case 1:

                    CreateDataGridView clientsdgv = new CreateDataGridView();
                    //clientsdgv.clientsDgv
                    Controls.Add(clientsdgv.clientsDgv);
                    clientsdgv.clientsDgv.BringToFront();


                    DesignDataGridView designdgv = new DesignDataGridView();

                    designdgv.ClientsDataGridFormatting(clientsdgv.clientsDgv);
                    designdgv.ClientsDataGridPosition(clientsdgv.clientsDgv, txtID);
                    //SetDoubleBuffered.SetDoubleBuffering(clientsdgv.clientsDgv, true);



                    if (string.IsNullOrEmpty(txtID.Text) || txtID.Text == "0")
                    {
                        clientsdgv.clientsDgv.DataSource = null;
                        clientsdgv.clientsDgv.Update();
                        clientsdgv.clientsDgv.Dispose();
                        clientsdgv.clientsDgv.Visible = false;
                        return;
                    }
                    else
                    {
                        GetSqlData getSqlData = new GetSqlData();

                        try
                        {
                            clientsdgv.clientsDgv.SuspendLayout();
                            columnName = "PersonalIDBulstat";
                            ID = txtID.Text;
                            clientsdgv.clientsDgv.Visible = true;
                            clientsdgv.clientsDgv.DataSource = getSqlData.SearchClientsInSql(columnName, ID);
                            clientsdgv.clientsDgv.Update();
                            clientsdgv.clientsDgv.ResumeLayout();
                        }
                        catch
                        {
                            title = "Clients";
                            SetMessageBoxTypes.MessageBoxContactAdminOk(title);
                        }
                    }
                    break;
            }
        }

提前谢谢!

1 个答案:

答案 0 :(得分:0)

这是我在加载表单时将dgvClients声明为DataGridView的工作代码:

vue-showdown