-->

Sunday, September 8, 2013

How to use Image in Button: WPF

Most often a button control is used with the content property and sometimes with an image in our programming fields. We have learnt about the content property of the button, now in this article we will place an image in a button control.

To place an image we have to sure first, that the image is in our project’s directory. Now write the following code in XAML just below the grid node (default placed node in a file).
<Grid>
<Button Width="200" Height="50">
<Button.Background>
<ImageBrush ImageSource="buttonImage.png"></ImageBrush>
</Button.Background>
</Button>
</Grid>

In the above code I have set the image named buttonImage.png as the background of the button. It will place a button with an image as in the following image.

Set Image as a background of a button: WPF

As we can see the image has been stretched in the button. To place the image as is, the following code will help you:
<Button Width="200" Height="50">
<Button.ContentTemplate>
<DataTemplate>
<Image Source="buttonImage.png"></Image>
</DataTemplate>              
</Button.ContentTemplate>          
</Button>

Run the project now and the image is placed as it is, the remaining space will be left blank.

Set image as a content template of a button: WPF C#

How to bind GridView using XML file in ASP.NET

Introduction about XML

XML stands for xtensible markup language. It have many features such as:
  • Xml is used for carry data .
  • Xml structure is based on tree structure.
  • Xml is not a presentation language 
  • XML use user defined tags for making structre.
  • Xml language is a case-sensitive language. 
  • XML is a Text File 
Now at a time if you want to make a structural database in xml file then you follow some steps . these steps are:
Step-1: First defined root node like <bookstore> 
Step-2: Declare child node <bookstore> <book>
Now you can consider book tag is your table name which you create in SQL and bookstore represent the database name.
Step-3 : Declare your column name inside the <book> tag such as Title of the book, author and price of the book .
now your file look like that.
Book store tree
Step-4 repeat your step 2-3 again
Now your complete code is

<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
  <book>
    <title>
      ASP.NET
    </title>
    <author>
      SCOTT
    </author>
    <price>
      20$
    </price>
  </book>
<book>
    <title>
      PHP
    </title>
    <author>
      JECOB
    </author>
    <price>
      20$
    </price>
  </book>


</bookstore>

Dataset

Dataset is a collection of data-Table. So easily you can bind any control with Dataset . Dataset is a class file which stored in System.Data namespace. Here in this example using Dataset we can read xml file from the source. 
lets take an example , How to bind GridView using XML file in ASP.NET


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default12.aspx.cs" Inherits="Default12" %>

<!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">
        </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;
using System.Data;

public partial class Default12 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("XMLFile.xml"));
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}
Output
How to bind GridView using XML file in ASP.NET

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#


© Copyright 2013 Computer Programming | All Right Reserved