Tag Archives: C# Windows Form

DataGridView meets Linq


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

  1. Create List of values
  2. 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.

Summing up values

txt_tqty.Text = qt.Sum(c => Convert.ToDouble(c)).ToString();

That’s all

Insert rows to DatagridView in C#.Net/.Net


DataGridView is an easiest option for reports as well as other data operations in C#.Net. In this post I will show how to use methods to add rows to a datagrid from controls like textbox.


We can add new rows to a datagridview in three different ways

  • Using a DataTable
  • Using Add Row method
  • Using Set Value method
With a Data Table

Create a data Table and Row and add it to the Grid using Add method of Rows.

         DataTable dt = new DataTable();
         DataRow dr;
         dataGridView1.DataSource = dt;
         dr = dt.NewRow();
         dt.Columns.Add("Column1");
         dt.Columns.Add("Column2");
         dr["column1"] = "Value1";
         dr["column2"] = "Value2";
         dt.Rows.Add(dr);
With param arguments

The Add method of row can be used to insert rows with array of objects as follows

dataGridView2.Rows.Add("Value1", "Values2");

With Set Value

Same as the above we can also use the SetValue method too

        int rid;
        try
        {
            rid = dataGridView2.Rows.Count;
            dataGridView2.Rows[rid].SetValues("value1", "Value2");
        }
        catch (ArgumentOutOfRangeException exc)
        {
            rid = dataGridView2.Rows.Add();
            dataGridView2.Rows[rid].SetValues("value1", "Value2");
        }

How to change the Back color of MDI Form – C#


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

MdiClient ctlMDI;

// Loop through all of the form's controls looking
 // for the control of type MdiClient.
 foreach (Control ctl in this.Controls)
 {
 try
 {
 // 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.
 }
 }

Place the code on Load Event of MDI Parent Form