-->

Thursday, September 24, 2015

Display sum of Columns total in GridView Footer in ASP.Net using C#

Introduction
In this article i will show you how to show total of all rows of GridView column. I will give you an example of this problem.Suppose you have a one numeric type columnn in your data table and you want to add all column values.Also display sum of column in footerRow of the GridView. If we think about algorithm for this type of problem. Algorithm say that first we take a Gridview with Two template  field , One template field have one item template and one footer template such as



  <asp:TemplateField HeaderText ="Name">
                <ItemTemplate >

                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("namet") %>'>

                    </asp:Label>
               


                </ItemTemplate>
                <FooterTemplate >
                    Total:
                 
                </FooterTemplate>
            </asp:TemplateField>

Another template field have same field which is take above such as

<asp:TemplateField HeaderText ="Income">
                <ItemTemplate>
               <asp:Label ID="incomelbl" runat="server" Text='<%# Eval("Income") %>' />

                </ItemTemplate>
                <FooterTemplate >
                   <asp:Label ID="footerlbl" runat="server" Text="" />
                 
                </FooterTemplate>


            </asp:TemplateField>


This whole part cover designing


In codebehind model first we would bind GridView on page Load method. After that handle RowDataBound event . In RowDataBound event  first check Row type is DataRow.

After that find income label in Gridview using findcontrol method.

 if (e.Row.RowType ==DataControlRowType .DataRow)
        {
            var incomsal = e.Row.FindControl("incomelbl") as Label;
            if (incomsal != null)
            {
                total += int.Parse(incomsal.Text);
            }
       
        }

Here Total is a integer variable.
Now Show Total income to footer label then you must find control from the footer row on Page_Load method

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page .IsPostBack)
        {
           binddata();
           var footlbl = GridView1.FooterRow.FindControl("footerlbl") as Label;
           if (footlbl != null)
           {
               footlbl.Text  = total.ToString();
           }
        }
   
    }

    After founded , Total value assign to footer label
    Run you program.

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

<!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" ShowFooter ="true" AutoGenerateColumns ="false" OnRowDataBound="GridView1_RowDataBound">
        <Columns >

            <asp:TemplateField HeaderText ="Name">
                <ItemTemplate >

                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("namet") %>'>

                    </asp:Label>
               


                </ItemTemplate>
                <FooterTemplate >
                    Total:
                 
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText ="Income">
                <ItemTemplate>
               <asp:Label ID="incomelbl" runat="server" Text='<%# Eval("Income") %>' />

                </ItemTemplate>
                <FooterTemplate >
                   <asp:Label ID="footerlbl" runat="server" Text="" />
                 
                </FooterTemplate>


            </asp:TemplateField>


        </Columns>   



    </asp:GridView>
        <asp:Label ID="result" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

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

public partial class additem : System.Web.UI.Page
{
    int total = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page .IsPostBack)
        {
           binddata();
           var footlbl = GridView1.FooterRow.FindControl("footerlbl") as Label;
           if (footlbl != null)
           {
               footlbl.Text  = total.ToString();
           }
        }
     
    }
    public void binddata()
    {
        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        con.Open();
        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
        cmd.CommandText = "Select * from [footerex]";
        cmd.Connection = con;
        System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
        System.Data.DataSet ds = new System.Data.DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType ==DataControlRowType .DataRow)
        {
            var incomsal = e.Row.FindControl("incomelbl") as Label;
            if (incomsal != null)
            {
                total += int.Parse(incomsal.Text);
            }
         
        }

    }
}

Output
How to display total in GridView Footer in ASP.NET

Wednesday, September 23, 2015

Example to create tooltip using code in windows form c#

Introduction
In this article i will show you , how to generate tooltip on controls using c# code. By using tooltip we can put some hints about task. I will give you an example of ToolTip in windows form c#. In this article we will use ToolTip class with their SetToolTip method.


Description

Code

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;

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

        private void settooltip()
        {
            ToolTip tip = new ToolTip();
            tip.SetToolTip(this.button1, "Default Push Button");
            tip.SetToolTip(this.textBox1, "Empty TextBox");
        }
    }
}

Change windows phone 8.1 app icon

Introduction
In this article i will show you how to change windows phone 8.1 app icon using manifest file. You can set your image file in place of default icon file. The default app icon file looking like cross square. I will provide you step by step guide to change the icon file or you can say image file.


  1. Double click on Package.appxmanifest file.
  2. Select Visual Assets tab.
  3. Add new image in Assets folder with 44x44 resolution.
  4. Change 44x44 resolution image file

Change windows phone 8.1 app icon

 Now, code generate the following icon

Change windows phone 8.1 app icon

Tuesday, September 22, 2015

Add column in WPF DataGrid using both xaml and code

Introduction
In this article i will show you how to add columns in DataGrid using xaml code and c# code. I will give you an example of add columns in WPF DataGrid. Datagrid is used to represent a tabular view of data in programming. It have multiple template designs to be customizable by the programmer as per the requirements. It supports some other features like sorting the data, dragging columns and sometimes editing when user needs to do.


To add a DataGrid, just drag-n-drop it from the toolbox. Now it is known as a tabular representation, write following code to add some columns (3 here):
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="First Column"/>
<DataGridTextColumn Header="Second Column"/>
<DataGridTextColumn Header="Third Column"/>
</DataGrid.Columns>
</DataGrid>

When we run this window then a Datagrid is shown with three columns like in below image:

How to add columns in DataGrid control in WPF

DataGrid provides columns sorting, reordering and sizing with some properties like CanUserReorderColumns, CanUserResizeColumns, CanUserSortColumns and etc. We can even perform grouping of data by adding a group description for the list to be bind.

To add a datagrid using code behind:
DataGrid dataGrid = new DataGrid();

DataGridTextColumn textColumn1 = new DataGridTextColumn();
textColumn1.Header = "First Column";
DataGridTextColumn textColumn2 = new DataGridTextColumn();
textColumn2.Header = "Second Column";
DataGridTextColumn textColumn3 = new DataGridTextColumn();
textColumn3.Header = "Third Column";

dataGrid.Columns.Add(textColumn1);
dataGrid.Columns.Add(textColumn2);
dataGrid.Columns.Add(textColumn3);

The above code snippet add a same datagrid with three columns as in above image. We will learn binding of datagrid in later articles.

Sunday, September 20, 2015

Example to change windows form shape using c#

Introduction
In this article i will show you, how to change the shape of the current form. The default form shape is square and i want to change it in ellipse form. Copy this code and paste into your code file.

Code

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;

namespace WindowsFormsApplication2
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            System.Drawing.Drawing2D.GraphicsPath shape1 = new System.Drawing.Drawing2D.GraphicsPath();
            shape1.AddEllipse(0, 0,this.Width, this.Height);
            this.Region = new System.Drawing.Region(shape1);
        }
    }
}
Code generate  the following output

Example to change windows form shape using c#

Saturday, September 19, 2015

Bind ASP.NET chart control from database table

Introduction
In this article i will show, how to bind chart control with database table in asp.net c#. I will give you example of it also provide video for implementation. A graph or chart diagram represents your set of data.If you want to show your company report then you must use chart control. There are different types of chart show different types of data such as



How to bind Chart Control in ASP.NET
Pie Chart
Using the pie-chart you can classify your data in graphical format. According to above diagram your movie are divided in different sections such as Action take 18% of whole movie as well as 11% take Horror.

Example of How to bind Chart Control using SqlDataSource
Step-1: Create Database Table in visual studio.

Chart Database table

.
Step-2: Bind connection with Database using SqlDataSource
<%@ Register assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.DataVisualization.Charting" tagprefix="asp" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1" OnLoad="Chart1_Load">
            <series>
                <asp:Series ChartType="Pie" Name="Series1" XValueMember="movie_type" YValueMembers="percentage">
                </asp:Series>
            </series>
            <chartareas>
                <asp:ChartArea Name="ChartArea1">
                    <Area3DStyle Enable3D="True" />
                </asp:ChartArea>
            </chartareas>
        </asp:Chart>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [movie]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Output
How to bind Chart Control in ASP.NET

Thursday, September 17, 2015

Edit menu bar with cut copy and paste operations in Windows Form C#

Introduction

In this article i will show you how to do cut copy and paste operation on text which is entered in the TextBox. I will give you an example of it. It is impossible for one, specially a Programmer, to imagine his/her life without editing. Editing operations (Cut/Copy and paste etc.) are performed using clipboard, so these are also called Clipboard operation.


Invention

Larry Tesler, who invented Cut/Copy and paste in 1973. He is a computer scientist, working in the field of human-computer interaction. Tesler has worked at Xerox PARC, Apple Computer, Amazon.com and Yahoo! We can find out more about LarryTesler.

In this article I will show how to perform these operations with our own code. As we are just doing editing operations, it doesn't matter which control we are using. Here I am using two textboxes to perform this operation. I created a simple window form with a menu strip and two textboxes. In the menu strip all the operations are added in Edit menu item as shown in below screenshot.



Generate the click event of above three menu items. 
Copy

We are going to copy the selected text of textbox, so we use SelectedText property of textbox. To copy the data in clipboard there is a method in Clipboard class i.e. SetData(), which will clear the clipboard and then add new data in specified format to clipboard.

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
      TextBox txtBox = this.ActiveControl as TextBox;
      if (txtBox.SelectedText != string.Empty)
         Clipboard.SetData(DataFormats.Text, txtBox.SelectedText);
Cut

Actually, cut operation is a combination of copying and removing that text from that location. So we will copy the text and empty that string after that.

private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
    TextBox txtBox = this.ActiveControl as TextBox;
    if (txtBox.SelectedText != string.Empty)
       Clipboard.SetData(DataFormats.Text, txtBox.SelectedText);
    txtBox.SelectedText = string.Empty;
Paste


To insert clipboard data to current location is called paste. We can perform a paste operation multiple times with a single clipboard data. To paste the data from clipboard there is a method in Clipboard class i.e. GetText(). First we get actual position of cursor in the textbox and then insert the data to that position using Insert() method as in above code.

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
    int position = ((TextBox)this.ActiveControl).SelectionStart;
    this.ActiveControl.Text = this.ActiveControl.Text.Insert(position, Clipboard.GetText());
}

Insert() is a method of string class, which return a string in which a specified string is inserted at a specified position. So that is how we perform editing operations with our own code. The same procedure can be used to perform these operation in other controls of windows forms.

Download source.
© Copyright 2013 Computer Programming | All Right Reserved