DataGridView is a tool for displaying, modifying, entering values in Visual Studio.Net. Usually we are using loops or readers to traverse through rows or columns.
Linq query make it possible to do these take with few lines, instead of complicated loops.
We have a data entry GridView which accepts purchase details such as name,qty,price etc. Lets get the sum of quantity supplied in the grid using Linq query
We do the following
Create List of values
Call the Sum() for total function for the column we specified
The Linq Querry
var qt = from DataGridViewRow row in dataGridView1.Rows where row.Cells[COL_QTY.Index].FormattedValue.ToString().Trim().Length!=0 && row.Cells[COL_QTY.Index].FormattedValue != null select ( row.Cells[COL_QTY.Index].FormattedValue.ToString().Trim());
We have to make sure the cell is not null or it has any values at all, we place where section, otherwise the System will fire you for an unhandled exception, lol.
As a C# programmer, you may have used Back Color property for changing the color of the background for control as well as Windows form. You can also found that this not works with MDI Parent form.
MDI is the container for child forms or control, actually, it’s inherited from the panel [leave it no…lol].
Ok, all you want to repaint all the controls in it, even though there are no controls were placed, think about the inherited panel. All we have to do is try to cast every control as MDIClient and loop through it and change the color as follows
// Loop through all of the form's controls looking
// for the control of type MdiClient.
foreach (Control ctl in this.Controls)
// Attempt to cast the control to type MdiClient.
ctlMDI = (MdiClient)ctl;
// Set the BackColor of the MdiClient control.
ctlMDI.BackColor = Color.Gold;
catch (InvalidCastException exc)
// Catch and ignore the error if casting failed.