Question

Basically I have the following code to bring up 3 different datagrid views depending on the button clicked.

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

}

So far so good, but the table that it brings up is all "smooshed" to the right

I've checked up on autosizing but haven't found how to apply it to my code specifically. I would like it to show all the columns, instead of just showing the first two and then giving a scrollbar at tha bootom.

I'm assuming here that autosizing is the right way to go, if not please set me on the right path.

Was it helpful?

Solution

Autosizing of a dataGridView should be on by default (and its probably set to 'displayed cells'. Does your first view of a dataGrid look fine (with all 3 columns showing up), but its subsequent views that appear scrunched?

In one of my applications, I handle the autosizing within the form1_load event.

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


    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top