0

基本上,我有以下代码可以根据单击的按钮显示 3 个不同的数据网格视图。

    public partial class ChooseDB : Form
    {
    private DataGridView doctorsDataGridView = new DataGridView();
    private DataGridView patientsDataGridView = new DataGridView();
    private DataGridView hospitalsDataGridView = new DataGridView();

    public ChooseDB()
    {
        InitializeComponent();
    }

    public void buttonDoctorsDB_Click(object sender, EventArgs e)
    {
        doctorsDataGridView.DataSource = doctorsDataSet.Doctors;
        doctorsDataGridView.Dock = DockStyle.Right;
        if (Controls.Contains(patientsDataGridView))
            Controls.Remove(patientsDataGridView);
        if (Controls.Contains(hospitalsDataGridView))
            Controls.Remove(hospitalsDataGridView);
        this.Controls.Add(doctorsDataGridView);
    }

    public void buttonPatientsDB_Click(object sender, EventArgs e)
    {
        patientsDataGridView.DataSource = patientsDataSet.Patients;
        patientsDataGridView.Dock = DockStyle.Right;
        if (Controls.Contains(doctorsDataGridView))
            Controls.Remove(doctorsDataGridView);
        if (Controls.Contains(hospitalsDataGridView))
            Controls.Remove(hospitalsDataGridView);
        this.Controls.Add(patientsDataGridView);
    }

    public void buttonHospitalsDB_Click(object sender, EventArgs e)
    {
        hospitalsDataGridView.DataSource = hospitalsDataSet.Hospitals;
        hospitalsDataGridView.Dock = DockStyle.Right;
        if (Controls.Contains(patientsDataGridView))
            Controls.Remove(patientsDataGridView);
        if (Controls.Contains(doctorsDataGridView))
            Controls.Remove(doctorsDataGridView);
        this.Controls.Add(hospitalsDataGridView);
    }
}

}

到目前为止一切都很好,但它带来的桌子都被“平滑”到了右边

我已经检查了自动调整大小,但还没有找到如何将它专门应用于我的代码。我希望它显示所有列,而不是只显示前两列,然后在 tha bootom 处给出一个滚动条。

我在这里假设自动调整大小是正确的方法,如果不是,请让我走上正确的道路。

4

1 回答 1

0

默认情况下应该启用 dataGridView 的自动调整大小(并且它可能设置为“显示的单元格”。您的 dataGrid 的第一个视图看起来不错(所有 3 列都显示),但它的后续视图看起来很皱?

在我的一个应用程序中,我在 form1_load 事件中处理自动调整大小。

private void Form1_Load(object sender, EventArgs e)
    {
        GetICD10();
        FreezeBand(dataGridView1.Columns[0]);   // Client requested to have ICD code column "frozen" by default

        // Cannot seem to select both autosize and allow user to size in designer, so below is the "code around".
        //  Designer has autosize set to displayedcells.
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;   // Turn off autosize
        dataGridView1.AllowUserToResizeRows = true;                                 // Turn on letting user size columns
        dataGridView1.AllowUserToOrderColumns = true;

        // Create tooltip and populate it
        var toolTip1 = new ToolTip { AutoPopDelay = 5000, InitialDelay = 1000, ReshowDelay = 500, ShowAlways = true };
        toolTip1.SetToolTip(tbCode, "Enter an ICD code to search for");
        toolTip1.SetToolTip(tbDescriptionLong, "Enter a description to search for");


    }
于 2013-07-01T18:28:18.027 回答