Link to home
Start Free TrialLog in
Avatar of novknow
novknow

asked on

Display last row of DataGridView control fully

I display last row of DataGridView control with this code:

...
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
dataGridView1.Show();
dataGridView1.AutoResize...
dataGridView1.Focus();

1) The first time the dataGridView1 is shown, the current cell is at the first row.
2) From second call onwards, the last row is displayed but partially.

How can I display the last row fully with the first execution of the code?

Avatar of Gautham Janardhan
Gautham Janardhan

can u explain a bit more
<
1) The first time the dataGridView1 is shown, the current cell is at the first row.
2) From second call onwards, the last row is displayed but partially
>
Avatar of novknow

ASKER

The program generate a report in table form (shown using datagridview) each time a user select his choice from the various controls - treeview,comboboxes,checkboxes,....
The report should display the last row (usually the total) of the datagridview each time it is shown.

1) Problem is, the last row is not shown when the datagridview is first display but when the user select another set of criteria and generate the report the second time, the last is shown correctly!

2) Now, when the last row is visible, it is only partially visible (only 80% of the row is able the bottom edge). It becomes fully visible only if the up arrow key is pressed once.

how u are filling the lower row can u post the code
SOLUTION
Avatar of tolgaong
tolgaong
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of novknow

ASKER

// constructor
            dataGridView1.Dock = DockStyle.Fill;
            dataGridView1.Hide();

// loading...
            dataGridView1.CellFormatting -= dataGridView1_CellFormatting;
            dataGridView1.CellFormatting += dataGridView1_CellFormatting;
            dataGridView1.CellMouseDown -= dataGridView1_CellMouseDown;
            dataGridView1.CellMouseDown += dataGridView1_CellMouseDown;
            dataGridView1.MouseDown -= dataGridView1_MouseDown;
            dataGridView1.MouseDown += dataGridView1_MouseDown;
            dataGridView1.MouseUp -= dataGridView1_MouseUp;
            dataGridView1.MouseUp += dataGridView1_MouseUp;
            dataGridView1.SelectionChanged -= dataGridView1_SelectionChanged;
            dataGridView1.SelectionChanged += dataGridView1_SelectionChanged;

// Fill DataGridView...
// SQL code
            dataGridView1.Columns.Clear();
            dataGridView1.DataSource = null;
            dataTable1.Clear();
            SqlDataAdapter dataAdapter1 = new SqlDataAdapter(cmd);

            try
            {
                dataAdapter1.Fill(dataTable1);
                dataGridView1.DataSource = dataTable1;      // fill new data all OEE Mode
            }
            catch (SystemException ex)
            {
                dataGridView1.Columns.Clear();
                dataGridView1.DataSource = null;
                dataGridView1.Hide();
            }

// Show DataGridView...
            dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            dataGridView1.GridColor = SystemColors.ActiveBorder;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
            dataGridView1.MultiSelect = true;
            dataGridView1.BackgroundColor = Color.Honeydew;
            dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;

            for (int x = 0; x < dataGridView1.ColumnCount; x++)
            {
                dataGridView1.Columns[x].SortMode = reportSort[x];
                dataGridView1.Columns[x].HeaderText = reportTitle[x];
                dataGridView1.Columns[x].DefaultCellStyle.Alignment = reportAlign[x];
            }

            dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
           
            if (dataGridView1.RowCount == 0)
                dataGridView1.Hide();
            else
            {
                dataGridView1.Show();
                dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                dataGridView1.Focus();
            }

--- The End

The Fill and Show function above were called repeatedly each time a new selection is set.
I suspect there is some other events occurred which I did not expect.
I will step through the code once I get back to work.

// constructor
            dataGridView1.Dock = DockStyle.Fill;
            dataGridView1.Hide();

// loading...
            dataGridView1.CellFormatting -= dataGridView1_CellFormatting;
            dataGridView1.CellFormatting += dataGridView1_CellFormatting;
            dataGridView1.CellMouseDown -= dataGridView1_CellMouseDown;
            dataGridView1.CellMouseDown += dataGridView1_CellMouseDown;
            dataGridView1.MouseDown -= dataGridView1_MouseDown;
            dataGridView1.MouseDown += dataGridView1_MouseDown;
            dataGridView1.MouseUp -= dataGridView1_MouseUp;
            dataGridView1.MouseUp += dataGridView1_MouseUp;
            dataGridView1.SelectionChanged -= dataGridView1_SelectionChanged;
            dataGridView1.SelectionChanged += dataGridView1_SelectionChanged;

// Fill DataGridView...
// SQL code
            dataGridView1.Columns.Clear();
            dataGridView1.DataSource = null;
            dataTable1.Clear();
            SqlDataAdapter dataAdapter1 = new SqlDataAdapter(cmd);

            try
            {
                dataAdapter1.Fill(dataTable1);
                dataGridView1.DataSource = dataTable1;      // fill new data all OEE Mode
            }
            catch (SystemException ex)
            {
                dataGridView1.Columns.Clear();
                dataGridView1.DataSource = null;
                dataGridView1.Hide();
            }

// Show DataGridView...
            dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            dataGridView1.GridColor = SystemColors.ActiveBorder;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
            dataGridView1.MultiSelect = true;
            dataGridView1.BackgroundColor = Color.Honeydew;
            dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;

            for (int x = 0; x < dataGridView1.ColumnCount; x++)
            {
                dataGridView1.Columns[x].SortMode = reportSort[x];
                dataGridView1.Columns[x].HeaderText = reportTitle[x];
                dataGridView1.Columns[x].DefaultCellStyle.Alignment = reportAlign[x];
            }

                       
            if (dataGridView1.RowCount == 0)
                dataGridView1.Hide();
            else
            {
                dataGridView1.Show();
                dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
                dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                dataGridView1.Focus();
            }

try this i moved < dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
>
to after dataGridView1.Show();

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of novknow

ASKER

Hi gauthampj, tolgaong,
Thanks.
On the second question:
The last row was shown but sometime it did not show "fully".
And if the user pressed the arrow keys, datagridview than adjusted the rows to show fully!
Is it because my datagridview is sitting inside a panel control with docking set to Dock.Fill?
Avatar of novknow

ASKER

I have tried not setting the DockStyle.Fill, it still didn't work.

Any suggestion?
when u do
 dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];

the focus should come to the last row