Thursday, July 30, 2009

How to use combo box in silverlight

After reading this article we will be able to use a comboobx in a Silverlight application as we are habitual of using it with the formal application. Normally we would like to have a combo box which will show the name of the article or any thing which we would like that our client should see but internally we like to use the ID the primary key or something else to use for the internal purpose. So here are the steps we are going to follow.

Creating the Design Page For the Combobox Application.
1. Open Visual Studio 2008 or above. Create new Project.
2. Select the language C#, Silverlight Project.
3.Name it Combox.
4.Click ok.
5.Leave the Rest Settings as default.
6.Now open MainPage.xaml. modify the grid add the HorizontalAlignment="Left"
VerticalAlignment="Top"
7. And now And now add the Textblock (Label) like this:
<TextBlock Height="17.09" Grid.Column="0" Grid.Row="0" Width="100.303" Canvas.Left="260" Canvas.Top="38.91" Text="Client Name" TextWrapping="Wrap"
Foreground
="#FF000000" FontSize="16" Margin="12,0,8,5" />
8. In the next column of grid Add the Combobox. like:
<ComboBox x:Name="cmbClient" Grid.Column="1" Grid.Row="0" SelectionChanged="cmbClient_SelectionChanged"></ComboBox>
9. Not going into more Designing Issues. Lets Finish it here. And Start Our Coding.
10. Creating a button so that onclick of that button we can get the
Information of the Client
<Button x:Name="btn" Click="btn_Click" Content="Get Info" Grid.Column="0" Grid.Row="1"></Button>

Now lets create a class with the following Member Properties
Client ID
Client Name
Client Address
Client Mobile number

Now open the MainPage.xaml.cs File And create the following Class
public class MyClients
{
#region Pubic Property
public int pkid;
public string ClientName { get; set; }
public string ClientAddress { get; set; }
public string ClientMobile { get; set; }
#endregion
}

In Next strep we will Creat a List<T> type of variables which will hold the List of MyClients to be displayed in the comboobx. Like:

public static List<MyClients> clientlist = new List<MyClients>();

Now next step is to create a function which will add the MyClients to the List Here is the function.

public voidAddClients()
{
MyClients mynewclient = new MyClients();
mynewclient.ClientAddress = "Jaipur";
mynewclient.ClientMobile = "1234567890";
mynewclient.ClientName = "Meetu Choudhary";
mynewclient.pkid = 1;
clientlist.Add(mynewclient);
//Adding 2nd CLient
mynewclient = newMyClients();
mynewclient.ClientAddress = "Jaipur";
mynewclient.ClientMobile = "1234567890";
mynewclient.ClientName = "Anita Choudhary";
mynewclient.pkid = 2;
clientlist.Add(mynewclient);
//Adding 3rd client
mynewclient = new MyClients();
mynewclient.ClientAddress = "Delhi";
mynewclient.ClientMobile = "1234567890";
mynewclient.ClientName = "Raman Mehta";
mynewclient.pkid = 3;
clientlist.Add(mynewclient);
//Adding 4th client
mynewclient = new MyClients();
mynewclient.ClientAddress = "Delhi";
mynewclient.ClientMobile = "1234567890";
mynewclient.ClientName = "Gaurav Arora";
mynewclient.pkid = 4;
clientlist.Add(mynewclient);
}

Now we need to call this function in the MainPage so the code of MainPage will now
look like
public MainPage()
{
InitializeComponent();
/*From here I added the code*/
AddClients();
//Telling the combobox which member to be displayed
cmbClient.DisplayMemberPath = "ClientName";
//Telling the Source of the List
cmbClient.ItemsSource = clientlist;
//This line will set the selected item
cmbClient.SelectedIndex = 1;
}

Now We will again declare a public static variable to access the object through out the page.
public static MyClients selectedcl;

Now setting the above variable on the selectionchanged event:
private void cmbClient_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
selectedcl = (MyClients)cmbClient.SelectedItem;
}

and Finally the code for the click event:

private void btn_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Client Name : " + selectedcl.ClientName + Environment.NewLine
+ "Client Address : " + selectedcl.ClientAddress + Environment.NewLine
+ "Client Mobile : " + selectedcl.ClientMobile + Environment.NewLine
+ "ID : " + selectedcl.pkid);
}


Now run and see the combox in active Code is attached
Download Code

Saturday, July 25, 2009

Google Code Search

Hey,

Just came across a great link to search codes by Google Here It is

http://www.google.co.in/codesearch

Hope you too will enjoy the searching

One More Great Morning

Meetu: Again a great morning of my life I received the Live meeting accounts and access to e-books
And The Videos Of my MVP Awards Are Already Uploaded Check Them At: My Forums

Thursday, July 16, 2009

Custom paging in gridview

This is a sample of customized Paging the concept implements the logic used in Google for paging the page numbers increases and decreases as in Google.. I have seen that logic and tires to implement it some how i succeeded and now i am sharing it with you all..

in this i have used a menu bar for the customized paging in the pager of a grid view....

the piece of code for the .cs file is as follows and for .aspx files and .cs files please go through the attachments..


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{

binding();
}
}
private void binding()
{
DataTable tblData = new DataTable();
DataColumn col1 = new DataColumn("Data1");
tblData.Columns.Add(col1);

for (int i = 1; i <= 1500; i++)
{
DataRow dr = tblData.NewRow();
dr["Data1"] = i;
tblData.Rows.Add(dr);
}
GridView1.DataSource = tblData;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
Menu pgmenu =(Menu) e.Row.FindControl("Menu1");
int startno, endno;
int selectedpgeno = -1;
if (ViewState["selectedpgno"] == null)
{
startno = 1;
endno = startno + 9;
}
else
{
selectedpgeno = Convert.ToInt32(ViewState["selectedpgno"]);
if ((selectedpgeno - 10) <= 0)
{
startno = 1;
endno = selectedpgeno + 9;
}
else
{
startno = selectedpgeno - 10;
endno = selectedpgeno + 9;
}
}

for (int i = startno; i <= endno; i++)
{
pgmenu.Items.Add(new MenuItem(i.ToString(), (i - 1).ToString()));
}
}
}
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
ViewState.Add("selectedpgno", e.Item.Text);
GridView1.PageIndex = Convert.ToInt32(e.Item.Value);
binding();
}
protected void Navigate(object sender, CommandEventArgs e)
{
if (e.CommandName == "Next")
{
ViewState.Add("selectedpgno", GridView1.PageIndex + 1);
GridView1.PageIndex = Convert.ToInt32(GridView1.PageIndex + 1);
binding();
}
}
}
Download file

Thanks and Regards
Meetu Choudhary
http://www.msdotnetmentor.com/

Subscribe via email

Enter your email address:

Delivered by FeedBurner

MSDotnetMentor

MSDotnetMentor My Website http://msdotnetmentor.com