显示datagridview中的所有列

本文关键字:datagridview 显示 | 更新日期: 2023-09-27 18:19:44

基本上,我有以下代码来根据单击的按钮显示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);
    }
}

}

到目前为止,一切都很好,但它提出的表格都被正确的"掩盖"了

我已经检查了自动调整,但还没有找到如何将其具体应用于我的代码。我希望它显示所有的列,而不是只显示前两列,然后在启动时提供滚动条。

我在这里假设自动缩放是正确的方式,如果不是,请让我走上正确的道路。

显示datagridview中的所有列

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");

    }