-->

Monday, September 16, 2013

How to Bind ComboBox with DataTable: WPF

DataTable is an object of ADO.NET library, which is used to store temporary data in tabular form in memory. When we are creating data table in code part, then we have to add some columns and have to add some rows.

We have learnt about the combo box introduction and its binding with the list of string in our earlier posts. In this article we will bind this combo box to a data table. Just add a combo box and write some properties as in below code:
<ComboBox Name="cmbBox" Height="25" Width="200"
ItemsSource="{Binding}"></ComboBox>

Now in code part, create an object of data table and add three columns i.e. Name, Age and Address, like in below c# code:
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Age");
dt.Columns.Add("City");

After adding columns, now its time to add some rows and insert some records in that. So write the below c# code in code part to add three rows:
DataRow dr = dt.NewRow();
dr["Name"] = "Jacob";
dr["Age"] = 25;
dr["City"] = "France";
dt.Rows.Add(dr);

DataRow dr1 = dt.NewRow();
dr1["Name"] = "Julia Martin";
dr1["Age"] = 26;
dr1["City"] = "France";
dt.Rows.Add(dr1);

DataRow dr2 = dt.NewRow();
dr2["Name"] = "Brandon";
dr2["Age"] = 24;
dr2["City"] = "London";
dt.Rows.Add(dr2);

We have successfully created a data table with three columns and add three rows with some valid data. To bind this data table to above combo box, we have to write these two lines in c# code part:

cmbBox.DataContext = dt;
cmbBox.DisplayMemberPath = dt.Columns[0].ToString();

The first line will assign the data context of combo box to the data table, while the second line will set the column name to be displayed in combo box. Run the project the check the combo box:

How to bind combo box with data table in WPF C#, XAML

How to Customize Status Bar in WPF

In earlier post, we have learnt about placing a status bar and add some items on that. WPF provides some advanced options with status bar i.e. we can add some more items or even containers in status bar. In this article we will add a stack panel, image and some more items in a status bar.

Just write the following code in our XAML code window:
<StatusBar Name="statusBar" VerticalAlignment="Bottom">
<StackPanel Orientation="Horizontal">
<StackPanel>
<StatusBarItem Margin="5" >Status 1</StatusBarItem>
<StatusBarItem Margin="5" >Status 2</StatusBarItem>
<StatusBarItem Margin="5" >Status 3</StatusBarItem>
</StackPanel>
<Image Margin="5" Source="wall.jpg" Width="100" Height="30"></Image>
<ProgressBar Width="150" Height="20" Name="progressBar" Value="45"
HorizontalAlignment="Right" Margin="5"></ProgressBar>
</StackPanel>
</StatusBar>

Run the project and it will show a window with three strings in a stack panel, an image and a progress bar. The first stack panel also contain a nested stack panel. These items are added just for example, not for any purpose.

Customize status bar in WPF


Just like the above code, we can place any control in the status bar, according to our requirements.

Status Bar Control: WPF

As the name implies, this control is used to show the status of our progress in our application. The status may be like line/column no. in Notepad, page no. in MS Word, or some text in visual studio. It is very easy to add a status bar in our WPF applications.

Just drag-n-drop the Status bar control on the window, it will display a horizontal bar on the window. It can contains strings, images or other containers like panels. A single item can be added using StatusBarItem in the control. The following XAML code will be used to add a status bar in a window.
<StatusBar Name="statusBar" VerticalAlignment="Bottom">
<StatusBarItem Name="statusBarItem">Value Inserted</StatusBarItem>          
</StatusBar>

In the above code the status bar has been placed on bottom by using VerticalAlignment property. When we run the code, it will add a status bar as shown in following image:

status Bar control introduction in WPF

To add a progress bar control with above string, just use the following code:
<StatusBar Name="statusBar" VerticalAlignment="Bottom">
<StatusBarItem Name="statusBarItem">Value Inserted</StatusBarItem>
<ProgressBar Width="150" Height="20" Name="progressBar" Value="45"
HorizontalAlignment="Right"></ProgressBar>
</StatusBar>

When we run the code, it will display a string on left with a progress bar on right side of the window.

Status bar control with string and progress bar in WPF

How to use RequiredFieldValidator on ListBox in ASP.NET

If you want to apply RequiredField Validator to ListBox control then you should drop Required FieldValidator on design window. In previous example how to use RequiredFieldValidator control.

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "you are selected <br/>" + ListBox1.SelectedItem.Text;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:ListBox ID="ListBox1" runat="server" Height="136px" Width="110px">
            <asp:ListItem>Apple </asp:ListItem>
            <asp:ListItem>Mango</asp:ListItem>
            <asp:ListItem>Orange</asp:ListItem>
        </asp:ListBox>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ListBox1" ForeColor="#FF3300">Select any one in given list</asp:RequiredFieldValidator>
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
 
    </div>
    </form>
</body>
</html>

Output
How to use RequiredFieldValidator on ListBox in ASP.NET

How to bind ListBox using SqlDataSource in ASP.NET

ListBox is a collection of string . If you want to bind ListBox to database using SqlDataSource then you can follow same step as a Gridview binding.

How to bind GridView using SqlDataSource in ASP.NET

After making connection string in web.config file your wizard asking about DataTextField and DataValue field.
Select DataText Field in SqlDataSource



<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1" DataTextField="namet" DataValueField="Id"></asp:ListBox>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [footerex]"></asp:SqlDataSource>
 
    </div>
    </form>
</body>
</html>

Output
How to bind ListBox using SqlDataSource  in ASP.NET

Sunday, September 15, 2013

Introduction to C# : Console Application Tutorial

In this tutorial we will learn basic structure of the c# program . A structure can hold multiple things such as namespaces , main method ,classes etc.
The basic structure of the c# program is

using System;
//import namespaces here

namespace ConsoleApplication4
{
    //create new namespace her also a namespace can contain sub namespace,classes,delegates etc
    class Program
    {
        //class start here . A class can hold data member and member function.  
        static void Main(string[] args)
        {
        }
    }
}


If you want to create a new c# application in visual studio then follow some steps for creating C# console application.
Step-1: Open visual studio
Step-2: Select file->new->project
Step-3: New Window will open , In Left pane you can select your language and right pane you can select application(console application)
Step-4: After selection (left pane-->VB and right pane -->Console application)
Step-5: now at time you can design your first c# application

If you want to write and read something like "hello world" on/from console window  then you should use Console class. This class provide static method for reading and writing . lets take an example if you want to write something on console window then use

Console.WriteLine("hello world");

Similarly if you want to read something from keyboard then use
  string n = Console.ReadLine();

  here is the complete code.
using System;
//import nameapces here

namespace ConsoleApplication4
{
    //create new namespace her also a namespace can contain sub namespace,classes,delegates etc
    class Program
    {
        //class start here . A class can hold data member and member function.
        static void Main(string[] args)
        {
        Console.WriteLine("hello world");
         string n = Console.ReadLine();
         Console.ReadKey();
        }
    }
}

 here is the first line define the declaration of the namespace.
 what is a namespace : The namespace is used to organize your code and is a collection of classes, interface, structs, enums and delegates.
 Main method: main method is the entry point into your application.
Output
Introduction to C# : Console Application Tutorial


Saturday, September 14, 2013

How to use ListBox Control in ASP.NET

The ListBox Control

The List control is a standard Web server control used to select one or more items from a list of items on a web page at runtime.
you can use the Rows property of the ListBox control to specify the height of the control. To enable muliple item selection you can set selection Mode property to ListSelectionMode. Multiple. you can find the
selection item in a single-selection ListBox control with the help of SelectedItem and SelectedIndex
properties. The SelectedItem property returns the  selection item as a List item object, which supports Text,
value and ListBox control and returns each as a List Box controls, the loop runs over the selected item
in the List Box controls and returns each selected item as a ListItem object by using the selectedIndex property.
This ListBox class inherits the :ListControl class and does not have any non-inherited events. The ListControl class does not have any non-inherited methods either. Note that the Items property of the ListControl class returns a collection of ListItem Objects which you can use to access an item in a ListBox.

Lets take a simple example of How to use ListBox Control.

In this example we will show that how to bind ListBox Control using Items collection property
Step-1 : Drop ListBox Control to Design window
Step-2 : Select Edit item using Show smart tag or you can use Items collection property for adding item to the ListBox
How to use ListBox Control in ASP.NET
Step-3 : Add item using ListItem Collection Editor
How to use ListBox Control in ASP.NET

Complete Code

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True">
            <asp:ListItem>Apple</asp:ListItem>
            <asp:ListItem>Mango</asp:ListItem>
            <asp:ListItem>Orange</asp:ListItem>
        </asp:ListBox>
    </div>
    </form>
</body>
</html>


Output
How to use ListBox Control in ASP.NET

© Copyright 2013 Computer Programming | All Right Reserved