-->

Sunday, September 8, 2013

How to Bind Gridview using Dictionary DataSource in ASP.NET

Introduction about Dictionary

If you know about array , array can store only single value or single type value  in the memory . It can't store double value in single memory location with different types. Suppose you want to store name and age of the person in single memory location then you must use collection . Here in this example we will use Dictionary collection.
The simple syntax of dictionary object are

Dictionary<type, type> dict =  new Dictionary<type, type>(); 
The above mention syntax is says to you that if you want to store person age with person name then you must use Dictionary
for example 



//create instance of the the Dictionary 
Dictionary<string,int> dict =new Dictionary<string,int>();

//Add item to the dictionary
dict.Add("my name is jacob", 26);
dict.Add("my name is lefore",27);

Now if you want to show of the data , You can use any data control such as GridView , FormView , ListView etc.

Lets take an simple example to bind GridView using Dictionary collection
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default11.aspx.cs" Inherits="Default11" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
 
    </div>
    </form>
</body>
</html>

CodeBehind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default11 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Dictionary<string, int> dict = new Dictionary<string, int>();
        dict.Add("My name is jacob", 26);
        dict.Add("My name is lefore", 27);
        dict.Add("My name is michel", 24);
        dict.Add("My name is lisa", 19);
        GridView1.DataSource = dict;
        GridView1.DataBind();


    }
}

Output
How to Bind Gridview using Dictionary DataSource in ASP.NET

Download Source Code

Saturday, September 7, 2013

How to sort Gridview Data in ASP.NET

About Sorting 

Sorting means rearranging data either ascending or descending data. you want to perform sorting of the data then you should apply technique to sort of the data . Various techniques are available in logic programming such as Bubble sort, insertion sort , quick sort , selection sort etc.
Basically GridView represents Data in 2D array. Graphical representation of 2D array is


How to sort Gridview Data in ASP.NET
Lets take an example , Firstly bind GridvView with database using the SqlDataSource. After binding the GridView you should select Enable Sorting Checkbox using ShowSmart Tag.

Enable Sorting Checkbox

Your complete Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default10.aspx.cs" Inherits="Default10" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                <asp:BoundField DataField="namet" HeaderText="namet" SortExpression="namet" />
                <asp:BoundField DataField="Income" HeaderText="Income" SortExpression="Income" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [footerex]"></asp:SqlDataSource>
 
    </div>
    </form>
</body>
</html>

Output
How to sort Gridview Data in ASP.NET

How to sort Gridview Data in ASP.NET

Bind ListView Control with Grid Resource: WPF

Grid resource provides a way to reuse the objects and values by other controllers, as we studied in previous posts. In this article we will bind the listview with Grid resource defined in XAML code:
<Grid.Resources>          
<x:ArrayExtension Type="{ x:Type sys:String}" x:Key="placesSource">
<sys:String>London</sys:String>
<sys:String>Italy</sys:String>
<sys:String>California</sys:String>
<sys:String>France</sys:String>
</x:ArrayExtension>
</Grid.Resources>

This resource is the same in our recent article about listbox binding with grid resource. Write the following code in XAML to bind the above resource:
<ListView Name="listView" ItemsSource="{StaticResource placesSource}">
</ListView>

When we run this project then it will bind the above data with listview as shown in following image:


To get selected item from this listview, write following code in the selectionChanged event of the listview:
private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selectedValue = listView.SelectedItem.ToString();
MessageBox.Show(selectedValue);
}

Run the code and change the selection, a message box will display with the selected value.

Introduction of ListView Control: WPF

I have discussed about Listbox control introduction, its binding with grid resource and a list of string, in my previous posts. Now the next is ListView control, which is inherited by the listbox class. It means it have all the features of listbox and included some new features also.

ListView control provides some layouts to display the data bound to it. A simple listview, with some width and height, can be coded by following XAML code:

<ListView Name="listView" Height="300" Width="200" />

A listview can contain any type of items, it may be string, a class or it may be from database. We can show some items as same as in listbox control like:
<ListView Name="listView" Height="300" Width="200" >
<ListView.Items>
<ListViewItem>First Item</ListViewItem>
<ListViewItem>Second Item</ListViewItem>
<ListViewItem>Third Item</ListViewItem>
<ListViewItem>Fourth Item</ListViewItem>
</ListView.Items>
</ListView>

The above XAML code will add a listview with four items as shown in following image. In the image no items have been selected. We can select the item with IsSelected property to true, the more containing this property to true, the more will get selected.

ListView control introduction in WPF C#

When we talk about the properties, it have much more like listbox and some extra i.e. View, which is used to define the infrastructure for the data. We can show the data in tabular format using gridview control in the view mode of listview control. In the next article we will bind this listview control.

How to create a Subdirectory in C#

To maintain or store files on our systems, we may need to create subdirectory within a directory. Now,let's see how to create a sub directory within an existing directory. Perform the following steps to create a subdirectory within an existing directory.

Complete code of How to create sub directory 



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication2
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string dirpath = textBox1.Text;
            DirectoryInfo dirinfo = new DirectoryInfo(dirpath);
            string newsub = textBox2.Text;
            dirinfo.CreateSubdirectory(newsub);
            label3.Text = "your subdirectory " + newsub + " is created  at " + dirpath;


        }
    }
}

Output
How to create a Subdirectory in C#

How to create a Subdirectory in C#


Friday, September 6, 2013

How to create a Directory in C#

Using The Directory class

The Directory class exposes static methods to create ,move and enumerate directories and subdirectories. This class is located under the System.IO namespace and is not inherited by any other class. Using the Directory class, we can accomplish a number of operations, such as moving a directory from one location to another and remaining a directory. We do not need to create objects of the Directory class because this class provides static methods. A drawback of the Directory class is that it takes longer time to execute, since every time we call a method, The Directory class performs a security check.

Take a Example of Create a new Directory

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication2
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = @"c:\" + textBox1.Text;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                label2 .Text =@"You directory has baan created in c:\";

            }
            else
            {
                if (Directory.Exists(path))
                {
                 
                    label2.Text = @"You directory already exist in c:\";

                }

            }
        }
    }
}


Output



How to create a Directory in C#How to create a Directory in C#

How to create a Directory in C#



How to Bind ComboBox with List: WPF

The same procedure will be follow up as in listbox binding. In the XAML define a combobox with some common properties you want as height, width and etc. Don’t forget the name property, because it will be used to assign itemsSource for this control.

<ComboBox Name="comboBox" Width="200"/>

Create a list of type string and add some items with Add() method of list. I have added the same items as in grid resources.
List<string> strList = new List<string>();
strList.Add("London");
strList.Add("Italy");
strList.Add("California");
strList.Add("France");
comboBox.ItemsSource = strList;

Just run the code and the same window will be shown with a combobox containing above four items.

Combobox binding with List control WPF c#

Combobox also have a property selectedIndex which is used to get or set the selected index of the control. To select an item by default we can use this property as:

comboBox.SelectedIndex = 2;

Write the above line of code just below the c# code, and California will be selected.
© Copyright 2013 Computer Programming | All Right Reserved