Showing posts with label C# Windows Forms. Show all posts
Showing posts with label C# Windows Forms. Show all posts

Thursday, November 29, 2012

C# Windows Application Update app.config file

In the Post C# Windows Application Reading app.config file, we saw on how to read the key values in the App.config file, there are situations where we will have to update the values in the app.config file dynamically from within the application. 

In this post we shall see on how to update the values in the app.config file from the application.



App.config file before the update.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="TestKey" value="OriginalTestValue" />
  </appSettings>

</configuration>

Code to Update the app.config file

XmlDocument
XmlDoc = new XmlDocument();
XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
XmlNode keyNode = XmlDoc.SelectSingleNode("/configuration/appSettings/add");
keyNode.Attributes[1].Value = "UpdatedTestValue";

XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

App.config file after the update.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="TestKey" value="UpdatedTestValue" />
  </appSettings>
</configuration>


Add a reference to the System.Xml Namespace, since we are parsing the app.Config file.
using System.Xml;

Adding a Custom Icon to C# Windows Application and Forms

C# Windows applications, by come with the default Application icon, many a times we will want to change the default icon to one which suites the type of application, in this post we shall see on how to change the default icon and set a custom icon.

1. Open the Windows Application project in Visual Studio.
2. Open the Project Properties.
3. In the Applications tab, Under the resources section, browse and select the custom icon file.

Email validation using ErrorProvider in C# Windows Application

C# Windows Application provides an ErrorProvider control, which is similar to the ValidationSummary control in Asp.Net, this control consolidates all the error messages in the form and alerts the user on the Form errors.

In this post we shall see on how to use the ErrorProvider control to validate Email in a C# Windows Form.

RegularExpression Validation using Regex class in C# Windows Application

Asp.Net provides a RegularExpression Validator to validate custom expressions, in Windows Forms there are no such validators. However we can make use of the methods in the Regex class to validate custom expressions in C# Windows forms.

In this post we shall see on how to use the Regex class to validate custom expressions in a C# Windows forms Application.

Wednesday, November 28, 2012

Adding Image Buttons to C# Windows Form

The C# Windows forms buttons are usually rectangular in shaper and take the default Grey color background, however there are instances where we need to make our forms more stylish, in this post we shall see on how to set Images to a Button in a C# Windows forms application.

1. Create a new / open an existing C# Windows application.
2. Create/Open the form which needs the Image Button.
3. Add a button to the Form
4. Set the following Properties to the Button (View -> Properties Window or Press F4 key)

Rounder Corner C# Windows Form

The C# Windows forms are usually rectangular and take the default Grey color background, however there are instances where we need to make our forms more stylish, in this post we shall see on how to set a Rounder Corner appearance to a C# Windows Form.

1. Create a new / open an existing C# Windows application.
2. Create/Open the form which
needs the Rounder Corner appearance.
3. Open the Properties of the form (View -> Properties Window or Press F4 key)
4. Set the
following Properties.

Monday, November 26, 2012

Combobox validation using ErrorProvider in C# Windows Application

C# Windows Application provides an ErrorProvider control, which is similar to the ValidationSummary control in Asp.Net, this control consolidates all the error messages in the form and alerts the user on the Form errors.

In this post we shall see on how to use the ErrorProvider control to validate a ComboBox in a C# Windows Form.

Integer validation using ErrorProvider in C# Windows Application

C# Windows Application provides an ErrorProvider control, which is similar to the ValidationSummary control in Asp.Net, this control consolidates all the error messages in the form and alerts the user on the Form errors.

In this post we shall see on how to use the ErrorProvider control to validate Integers (Whole Numbers) in a C# Windows Form.

Friday, November 23, 2012

Number validation using ErrorProvider in C# Windows Application

C# Windows Application provides an ErrorProvider control, which is similar to the ValidationSummary control in Asp.Net, this control consolidates all the error messages in the form and alerts the user on the Form errors.

In this post we shall see on how to use the ErrorProvider control to validate Numbers in a C# Windows Form.

C# Windows Application Form validation using ErrorProvider


C# Windows Application provides an ErrorProvider control, which is similar to the ValidationSummary control in Asp.Net, this control consolidates all the error messages in the form and alerts the user on the Form errors.

In this post we shall see on the basics of the ErrorProvider control, the successive posts will show on how to use the control to perform various types of validations.

Thursday, November 22, 2012

Binding data to a ListView with Multiple Columns in C# Windows Form

A ListBox control in a C# windows forms, can display only one column of data, but there are instances when we want to display multiple columns of data. One option is to use the DataGrid control, but we can also use the ListView control to display multiple column data.

The
ListView can be used to display static information, which the user will not be changing, in this post we shall see on how to use the ListView control to display multiple columns of data in a C# Windows Form. We shall bind the data from the list view from the code behind file.

C# Windows Forms ListView with multiple columns

A ListBox control in a C# windows forms, can display only one column of data, but there are instances when we want to display multiple columns of data. One option is to use the DataGrid control, but we can also use the ListView control to display multiple column data.

The
ListView can be used to display static information, which the user will not be changing, in this post we shall see on how to use the ListView control to display multiple columns of data in a C# Windows Form.

Tuesday, November 20, 2012

Adding TabControl to a C# Windows Form

A TabControl in a C# Windows Form allows us to group controls of different type in different tab panes in the same Form, the user can navigate between various tabs and access the controls in each of the Tabs.

A
TabControl is used when there are multiple controls to be displayed in a single form, TabControls helps in aligning the controls into groups and provides a better UI design to the form, in this post we shall see on how to add TabControls to a C# Windows Form.

Creating MDI Child Forms

MDI (Multiple-Document Interface) applications should have a MDI Parent form which will act as the parent for all other forms in the application. In this post we shall see on how to create a MDI Child form in a C# Windows Application and associate it with the MDI Parent form.

First let us create the MDI Parent Form.

Add Toolbar to a MDI Parent Form

MDI (Multiple-Document Interface) applications should have a MDI Parent form which will act as the parent for all other forms in the application. In this post we shall see on how to add a Toolbar control to an MDI Parent Form.

Monday, November 19, 2012

Add Menu to MDI Parent Form

MDI (Multiple-Document Interface) applications should have a MDI Parent form which will act as the parent for all other forms in the application. In this post we shall see on how to add a Menu control to an MDI Parent Form.

Creating MDI Parent Forms

MDI (Multiple-Document Interface) applications should have a MDI Parent form which will act as the parent for all other forms in the application. In this post we shall see on how to create a MDI Parent form in a C# Windows Application.

1. Create a new / open an existing C# Windows application.
2. Create/Open the form which needs to be set as the MDI Parent form.
3. Open the Properties of the form (View -> Properties Window or Press F4 key)
4. Set the IsMdiContainer property to true.
5. Notice that the Form UI Changes as follows.


MDI Applications (Multiple-Document Interface)

A C# Windows application usually allows only a single Form to be opened at any given point in time, this works fine for smaller applications, but when we need to develop larger applications, we need a comprehensive menu structure to navigate through the various forms, this is where the MDI Applications help us.

Multiple-Document Interface (MDI) application, are those which have a MDI parent form, this form will be the parent of all the other forms in the Application, typically this form will contain the Menu which is used to navigate to the other forms in the application. 

A form in an MDI application is set as the MDI Parent by setting the IsMDIContainer property to true. Once this property is set to true, this form will act as the Parent form for the application. We can add Menu control to this form.

Wednesday, June 20, 2012

Get Deleted rows in a DataTable / DataGridView in C# Windows Forms


In this post we shall see on how to get the rows which are Deleted in a DataGridView in a C# Windows forms application.

Unlike web applications, the Win Apps allow users to perform multiple operations like INSERT, MODIFY, DELETE to the data rows which are loaded in the DataGridView, once done the user clicks the Save button to send all the updates to the database at one shot.

Here we will see on how to capture the details of the rows which were deleted by the user.
Initially load the DataGridView with data and allow the user to work on the data.

private void btnLoadEmployees_Click(object sender, EventArgs e)
{
    // Get the Connection string from App.config.
    string strConn = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
    //
    // Query the DB to get the Data           
    SqlConnection objConn = new SqlConnection(strConn);
    SqlCommand objCmd = new SqlCommand("SELECT * FROM EMPLOYEE", objConn);
    SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
    DataSet dsEmployee = new DataSet();
    objDA.Fill(dsEmployee, "dtEmployee");
    //
    // Populate the GridView with Data
    gvEmployees.DataSource = dsEmployee.Tables["dtEmployee"];
}

Once the user is down with all the chages, he will click on the Save button, the following code filters the deleted rows and deleted the details from database by forming DELETE queries.

private void btnSave_Click(object sender, EventArgs e)
{
    string strQuery = string.Empty;
    DataTable dtChanges;
    //
    // Get the Updated DataTable back from the DataGridView
    DataTable dtEmployee = (DataTable)gvEmployees.DataSource;
    //
    // Get the Connection string from App.config.
    string strConn = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
    SqlConnection objConn = new SqlConnection(strConn);
    SqlCommand objCmd = new SqlCommand();
    //
    // Get the Deleted Rows by filtering on their RowState
    dtChanges = dtEmployee.GetChanges(DataRowState.Deleted);
    if (dtChanges != null)
    {
        //
        // Form the DELETE Query to Delete the Rows.
        objConn.Open();
        objCmd.Connection = objConn;
        for (int i = 0; i < dtChanges.Rows.Count; i++)
        {
            strQuery = "DELETE FROM Employee ";
            strQuery += "WHERE ID = '" + dtChanges.Rows[i]["ID", DataRowVersion.Original].ToString() + "'";
            //
            // Execute the Insert Query.
            objCmd.CommandText = strQuery;
            objCmd.ExecuteNonQuery();
        }
        objConn.Close();
        dtChanges = null;
    }
}

The DataTable.GetChanges(DataRowState.Deleted), returns a DataTable which contains the detials of all the rows which were deleted in the DataGridView.

Notice that we are using DataRowVersion.Original, along with the column name ID, this is because the row is deleted and the values from the deleted row cannot be obtained.

If we use dtChanges.Rows[i]["ID"], instead of dtChanges.Rows[i]["ID", DataRowVersion.Original], then the following exception will be thrown.

System.Data.DeletedRowInaccessibleException: Deleted row information cannot be accessed through the row.

To see on how to get the newly added rows refer the post Get new rows in a DataTable / DataGridView in C# Windows Forms. 
To see on how to get the Modified rows refer the post Get Modified rows in a DataTable / DataGridView in C# Windows Forms.