显示datagridview中的所有列

时间:2013-06-25 09:32:52

标签: c# datagridview autosize

基本上我有以下代码来显示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中给出一个滚动条。

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

1 个答案:

答案 0 :(得分:0)

默认情况下应该启用dataGridView的自动调整(并且它可能设置为'显示的单元格'。您对dataGrid的第一个视图是否正常(所有3列都显示出来),但是它的后续视图看起来是否已经过了?< / p>

在我的一个应用程序中,我在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");


    }