RSS

Category Archives: C#.NET

Multiplication of two columns in gridview and display the result in third column with footer dynamically

We can display the multiplication result of two column value in third column and net total in footer of gridview.

In .aspx page,

<asp:GridView ID=”GridView1″ runat=”server”>
<Columns>
<asp:TemplateField HeaderText=”Price”>
<ItemTemplate>
<asp:TextBox ID=”txtPrice” runat=”server” onkeyup=”GetSelectedRow(this)” ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Qty”>
<ItemTemplate>
<asp:TextBox ID=”txtQty” runat=”server” onkeyup=”GetSelectedRow(this)”></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Total”>
<ItemTemplate>
<asp:Label ID=”lblTotal” runat=”server”></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID=”Label1″ runat=”server” Text=”Total”></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

In Javascript,

function GetSelectedRow(txt) {
var row = txt.parentNode.parentNode;
var price = Number(row.cells[0].getElementsByTagName(“input”)[0].value);
var qty = Number(row.cells[1].getElementsByTagName(“input”)[0].value);
var Total = price * qty;
row.cells[2].getElementsByTagName(“span”)[0].innerHTML = Total;
var NetTotal = 0;
var grid = document.getElementById(“<%=GridView1.ClientID%>”);
var gridLength = grid.rows.length;
for (i = 1; i < gridLength; i++) {
if (grid.rows[i].cells[0].colSpan == 1) {
if (typeof (grid.rows[i].cells[2].text) != ‘undefined’) // This checking is for to support in all browser
number = grid.rows[i].cells[2].text;
else if (typeof (grid.rows[i].cells[2].textContent) != ‘undefined’)
number = grid.rows[i].cells[2].textContent;
else if (typeof (grid.rows[i].cells[2].innerText) != ‘undefined’)
number = grid.rows[i].cells[2].innerText;
//if (!isNaN(number))
NetTotal = Number(NetTotal) + Number(number);
}
}
GrandTotal = Number(NetTotal) – Number(number);
var footerText = (grid.rows[gridLength – 1].cells[2]);
if (typeof (footerText.text) != ‘undefined’)
footerText.text = GrandTotal;
else if (typeof (footerText.textContent) != ‘undefined’)
footerText.textContent = GrandTotal;
else if (typeof (footerText.innerText) != ‘undefined’)
footerText.innerText = GrandTotal;
}
}

 
Leave a comment

Posted by on September 1, 2012 in C#.NET, VB.NET

 

Nested GridView in C#.Net

Inside the gridview’s templatefield of itemtemplate you can add one more gridview.

In .aspx page,

<asp:GridView ID=”GridView1″ runat=”server”>
<Columns>
<asp:BoundField DataField=”SNo” HeaderText=”S.No”/>
<asp:BoundField DataField=”CategoryName” HeaderText=”Product Name”/>
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID=”GridView2″ runat=”server” AutoGenerateColumns=”false”>
<Columns>
<asp:BoundField DataField=”SNo” HeaderText=”S.No”/>
<asp:BoundField DataField=”CategoryName” HeaderText=”Product Name”/>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

In code behind,

protected void Page_Load(object sender, EventArgs e)
{
da.Fill(dt);
GridView1.DataSource = dt; // Data source for outer grid
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gv = (GridView)e.Row.FindControl(“GridView2”); //Finding inner grid
DataTable dt = new DataTable();
da.Fill(dt);
gv.DataSource = dt; // Data source for inner grid.
gv.DataBind();
}
}

 
Leave a comment

Posted by on September 1, 2012 in C#.NET

 

Send email using ASP.NET

ASP.NET allows us to send email from the programming.

using System.Net.Mail;

The above namespace having the feature to send email. Include that namespace to coding.

public void SendEmail()

{

 SmtpClient smtp = new SmtpClient();

 MailMessage mail = new MailMessage();

 mail.To.Add(“ToMailID@mail.com”);

 mail.From = new MailAddress(“FromMailID@mail.com”);

 mail.Subject = “Subject Here”;

 mail.Body = “Message Here”;

 smtp.Host = “smtp.mail.com”;

 smtp.Credentials = new System.Net.NetworkCredential(“FromId@mail.com”,”Password”);

 smtp.EnableSsl = true;

 smtp.Send(mail);

}

By the above way we can send email..

Good Luck……………

 
Leave a comment

Posted by on May 29, 2012 in ASP.NET, C#.NET

 

Read word by word from txt file in c#

The following is the c# code for reading the text file word by word.

        string a;
        string[] words = { “” };
        StreamReader Reader = new StreamReader(path);
        string[] s = File.ReadAllLines(path);
        while ((a = Reader.ReadLine()) != null)
        {
            words = a.Split(‘ ‘);
            foreach (string word in words)
                          Console.WriteLine(word);
        }

By this code, you can find the exact count of words, find exact certain length word(i.e. 5 letter word) and much more.

Good luck…………………………………..

 
Leave a comment

Posted by on May 19, 2012 in ASP.NET, C#.NET