Program
Reverce Array String : public static stringReverseString1 (string str)
{
char[] chars = str.ToCharArray();
char[] result = newchar[chars.Length];
for (int i =0, j = str.Length - 1; i < str.Length; i++, j--)
{
result[i] = chars[j];
}
return new string(result);
}
int countSpaces = mystring.Count(Char.IsWhiteSpace); // 6
int countWords = mystring.Split().Length; // 7
Count Character : int count = myString.TakeWhile(c => c == '$').Count();
var count = mystring.Count(x => x == '$')
Program --- #######################
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("[src*=plus]").live("click", function () {
$(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
$(this).attr("src", "images/minus.png");
});
$("[src*=minus]").live("click", function () {
$(this).attr("src", "images/plus.png");
$(this).closest("tr").next().remove();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="lblMessage" runat="server" Text="" ForeColor="Red"></asp:Label>
<br />
<div>
<asp:GridView runat="server" ID="grdCustomer" AutoGenerateColumns="False" ShowFooter="True" AllowSorting="True"
CssClass="grid" AllowPaging="True"
DataKeyNames="CustomerId" CellPadding="4" ForeColor="#333333" OnSorting="grdCustomer_Sorting" OnRowDataBound="grdCustomer_RowDataBound" OnRowCancelingEdit="grdCustomer_RowCancelingEdit" OnRowEditing="grdCustomer_RowEditing" OnRowCommand="grdCustomer_RowCommand">
<AlternatingRowStyle BackColor="#C3A2DB" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt="" style="cursor: pointer" src="images/plus.png" />
<asp:Panel ID="pnlOrders" runat="server" Style="display: none">
<asp:GridView ID="grdProduct" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="ProductName" HeaderText="Product Name" />
<asp:BoundField ItemStyle-Width="150px" DataField="Price" HeaderText="Price" />
<asp:BoundField ItemStyle-Width="150px" DataField="Quantity" HeaderText="Quantity" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name" SortExpression="CustomerName">
<ItemTemplate>
<asp:Label ID="lblCustomerName" runat="server" Text='<%# Bind("CustomerName") %>'></asp:Label>
<%--<asp:LinkButton runat="server" ID="lnkCustomerName" Text='<%# Bind("CustomerName") %>'></asp:LinkButton>--%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("CustomerName") %>' MaxLength="50"></asp:TextBox>
<%-- <asp:RequiredFieldValidator ID="valFirstName" runat="server" ControlToValidate="txtFirstName"
Display="Dynamic" ErrorMessage="First Name is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="editGrp">*</asp:RequiredFieldValidator>--%>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer City" SortExpression="City">
<ItemTemplate>
<asp:Label ID="lblCity" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCity" runat="server" Text='<%# Bind("City") %>' MaxLength="50"></asp:TextBox>
<%-- <asp:RequiredFieldValidator ID="valCity" runat="server" ControlToValidate="txtCity"
Display="Dynamic" ErrorMessage="City is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="editGrp">*</asp:RequiredFieldValidator>--%>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact Number">
<ItemTemplate>
<asp:Label ID="lblContactNo" runat="server" Text='<%# Bind("ContactNo") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtContactNo" runat="server" Text='<%# Bind("ContactNo") %>' MaxLength="50"></asp:TextBox>
<%-- <asp:RequiredFieldValidator ID="valContactNo" runat="server" ControlToValidate="txtContactNo"
Display="Dynamic" ErrorMessage="First Name is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="editGrp">*</asp:RequiredFieldValidator>--%>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Amount">
<ItemTemplate>
<asp:Label ID="lblAmount" runat="server" Text="0"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandArgument='<%# Bind("CustomerId") %>' CommandName="Edit" Text="EDIT"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="false" CommandName="Edit" Visible="false" Text=""></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
######################################################################3
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Annet
{
public partial class CustomerMaster1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
grdCustomer.PageIndexChanging += grdCustomer_PageIndexChanging;
if (!IsPostBack)
{
BindGrid();
ViewState["SortDirection"] = "ASC";
}
lblMessage.Text = "";
}
void grdCustomer_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdCustomer.PageIndex = e.NewPageIndex;
BindGrid();
}
void BindGrid()
{
using (var Context = new SampleEntities())
{
var query = Context.CustomerMasters.Select(m => m);
grdCustomer.DataSource = query.ToList();
grdCustomer.AllowPaging = true;
grdCustomer.PageSize = 3;
grdCustomer.DataBind();
}
}
protected void grdCustomer_Sorting(object sender, GridViewSortEventArgs e)
{
string Direction = ViewState["SortDirection"].ToString();
switch (e.SortExpression)
{
case "CustomerName":
using (var Context = new SampleEntities())
{
if (Direction == "ASC")
{
var query = Context.CustomerMasters.OrderBy(c => c.CustomerName);
grdCustomer.DataSource = query.ToList();
}
else
{
var query = Context.CustomerMasters.OrderByDescending(c => c.CustomerName);
grdCustomer.DataSource = query.ToList();
}
}
break;
case "City":
using (var Context = new SampleEntities())
{
if (Direction == "ASC")
{
var query = Context.CustomerMasters.OrderByDescending(c => c.City);
grdCustomer.DataSource = query.ToList();
}
else
{
var query = Context.CustomerMasters.OrderByDescending(c => c.City);
grdCustomer.DataSource = query.ToList();
}
}
break;
}
grdCustomer.AllowPaging = true;
grdCustomer.PageSize = 3;
grdCustomer.DataBind();
if (ViewState["SortDirection"].ToString() == "ASC")
{
ViewState["SortDirection"] = "DEC";
}
else if (ViewState["SortDirection"].ToString() == "DEC")
{
ViewState["SortDirection"] = "ASC";
}
// BindGrid("CustomerName", "ASC");
}
protected void grdCustomer_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int customerId = Convert.ToInt32(grdCustomer.DataKeys[e.Row.RowIndex].Value);
GridView gvProduct = e.Row.FindControl("grdProduct") as GridView;
LinkButton lblUnitsInStock = (LinkButton)e.Row.FindControl("LinkButton2");
if (lblUnitsInStock.Text == "editmode")
{
e.Row.RowState = DataControlRowState.Edit;
}
using (var Context = new SampleEntities())
{
var query = Context.ProductMasters.Where(c => c.CustomerId == customerId);
gvProduct.DataSource = query.ToList();
DataTable dt = new DataTable();
dt.Columns.Add("Total", typeof(int));
foreach (var item in query)
{
dt.Rows.Add(item.Price * item.Quantity);
}
object sumObject;
sumObject = dt.Compute("Sum(Total)", "");
e.Row.Cells[4].Text = sumObject.ToString();
gvProduct.DataBind();
}
}
}
protected void grdCustomer_RowEditing(object sender, GridViewEditEventArgs e)
{
grdCustomer.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void grdCustomer_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdCustomer.EditIndex = -1;
BindGrid();
}
protected void grdCustomer_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
int index = Convert.ToInt32(e.CommandArgument);
//Button Btn = (Button)e.CommandSource;
GridViewRow row = grdCustomer.Rows[index];
LinkButton txtLastName = row.FindControl("LinkButton2") as LinkButton;
txtLastName.Text = "editmode";
BindGrid();
// BoundField bb = (BoundField)row.Cells[0].Controls[1];
}
}
}
}
Reverce Array String : public static stringReverseString1 (string str)
{
char[] chars = str.ToCharArray();
char[] result = newchar[chars.Length];
for (int i =0, j = str.Length - 1; i < str.Length; i++, j--)
{
result[i] = chars[j];
}
return new string(result);
}
int countSpaces = mystring.Count(Char.IsWhiteSpace); // 6
int countWords = mystring.Split().Length; // 7
Count Character : int count = myString.TakeWhile(c => c == '$').Count();
var count = mystring.Count(x => x == '$')
Program --- #######################
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("[src*=plus]").live("click", function () {
$(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
$(this).attr("src", "images/minus.png");
});
$("[src*=minus]").live("click", function () {
$(this).attr("src", "images/plus.png");
$(this).closest("tr").next().remove();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="lblMessage" runat="server" Text="" ForeColor="Red"></asp:Label>
<br />
<div>
<asp:GridView runat="server" ID="grdCustomer" AutoGenerateColumns="False" ShowFooter="True" AllowSorting="True"
CssClass="grid" AllowPaging="True"
DataKeyNames="CustomerId" CellPadding="4" ForeColor="#333333" OnSorting="grdCustomer_Sorting" OnRowDataBound="grdCustomer_RowDataBound" OnRowCancelingEdit="grdCustomer_RowCancelingEdit" OnRowEditing="grdCustomer_RowEditing" OnRowCommand="grdCustomer_RowCommand">
<AlternatingRowStyle BackColor="#C3A2DB" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt="" style="cursor: pointer" src="images/plus.png" />
<asp:Panel ID="pnlOrders" runat="server" Style="display: none">
<asp:GridView ID="grdProduct" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="ProductName" HeaderText="Product Name" />
<asp:BoundField ItemStyle-Width="150px" DataField="Price" HeaderText="Price" />
<asp:BoundField ItemStyle-Width="150px" DataField="Quantity" HeaderText="Quantity" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name" SortExpression="CustomerName">
<ItemTemplate>
<asp:Label ID="lblCustomerName" runat="server" Text='<%# Bind("CustomerName") %>'></asp:Label>
<%--<asp:LinkButton runat="server" ID="lnkCustomerName" Text='<%# Bind("CustomerName") %>'></asp:LinkButton>--%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("CustomerName") %>' MaxLength="50"></asp:TextBox>
<%-- <asp:RequiredFieldValidator ID="valFirstName" runat="server" ControlToValidate="txtFirstName"
Display="Dynamic" ErrorMessage="First Name is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="editGrp">*</asp:RequiredFieldValidator>--%>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer City" SortExpression="City">
<ItemTemplate>
<asp:Label ID="lblCity" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCity" runat="server" Text='<%# Bind("City") %>' MaxLength="50"></asp:TextBox>
<%-- <asp:RequiredFieldValidator ID="valCity" runat="server" ControlToValidate="txtCity"
Display="Dynamic" ErrorMessage="City is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="editGrp">*</asp:RequiredFieldValidator>--%>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact Number">
<ItemTemplate>
<asp:Label ID="lblContactNo" runat="server" Text='<%# Bind("ContactNo") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtContactNo" runat="server" Text='<%# Bind("ContactNo") %>' MaxLength="50"></asp:TextBox>
<%-- <asp:RequiredFieldValidator ID="valContactNo" runat="server" ControlToValidate="txtContactNo"
Display="Dynamic" ErrorMessage="First Name is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="editGrp">*</asp:RequiredFieldValidator>--%>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Amount">
<ItemTemplate>
<asp:Label ID="lblAmount" runat="server" Text="0"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandArgument='<%# Bind("CustomerId") %>' CommandName="Edit" Text="EDIT"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="false" CommandName="Edit" Visible="false" Text=""></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
######################################################################3
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Annet
{
public partial class CustomerMaster1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
grdCustomer.PageIndexChanging += grdCustomer_PageIndexChanging;
if (!IsPostBack)
{
BindGrid();
ViewState["SortDirection"] = "ASC";
}
lblMessage.Text = "";
}
void grdCustomer_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdCustomer.PageIndex = e.NewPageIndex;
BindGrid();
}
void BindGrid()
{
using (var Context = new SampleEntities())
{
var query = Context.CustomerMasters.Select(m => m);
grdCustomer.DataSource = query.ToList();
grdCustomer.AllowPaging = true;
grdCustomer.PageSize = 3;
grdCustomer.DataBind();
}
}
protected void grdCustomer_Sorting(object sender, GridViewSortEventArgs e)
{
string Direction = ViewState["SortDirection"].ToString();
switch (e.SortExpression)
{
case "CustomerName":
using (var Context = new SampleEntities())
{
if (Direction == "ASC")
{
var query = Context.CustomerMasters.OrderBy(c => c.CustomerName);
grdCustomer.DataSource = query.ToList();
}
else
{
var query = Context.CustomerMasters.OrderByDescending(c => c.CustomerName);
grdCustomer.DataSource = query.ToList();
}
}
break;
case "City":
using (var Context = new SampleEntities())
{
if (Direction == "ASC")
{
var query = Context.CustomerMasters.OrderByDescending(c => c.City);
grdCustomer.DataSource = query.ToList();
}
else
{
var query = Context.CustomerMasters.OrderByDescending(c => c.City);
grdCustomer.DataSource = query.ToList();
}
}
break;
}
grdCustomer.AllowPaging = true;
grdCustomer.PageSize = 3;
grdCustomer.DataBind();
if (ViewState["SortDirection"].ToString() == "ASC")
{
ViewState["SortDirection"] = "DEC";
}
else if (ViewState["SortDirection"].ToString() == "DEC")
{
ViewState["SortDirection"] = "ASC";
}
// BindGrid("CustomerName", "ASC");
}
protected void grdCustomer_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int customerId = Convert.ToInt32(grdCustomer.DataKeys[e.Row.RowIndex].Value);
GridView gvProduct = e.Row.FindControl("grdProduct") as GridView;
LinkButton lblUnitsInStock = (LinkButton)e.Row.FindControl("LinkButton2");
if (lblUnitsInStock.Text == "editmode")
{
e.Row.RowState = DataControlRowState.Edit;
}
using (var Context = new SampleEntities())
{
var query = Context.ProductMasters.Where(c => c.CustomerId == customerId);
gvProduct.DataSource = query.ToList();
DataTable dt = new DataTable();
dt.Columns.Add("Total", typeof(int));
foreach (var item in query)
{
dt.Rows.Add(item.Price * item.Quantity);
}
object sumObject;
sumObject = dt.Compute("Sum(Total)", "");
e.Row.Cells[4].Text = sumObject.ToString();
gvProduct.DataBind();
}
}
}
protected void grdCustomer_RowEditing(object sender, GridViewEditEventArgs e)
{
grdCustomer.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void grdCustomer_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdCustomer.EditIndex = -1;
BindGrid();
}
protected void grdCustomer_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
int index = Convert.ToInt32(e.CommandArgument);
//Button Btn = (Button)e.CommandSource;
GridViewRow row = grdCustomer.Rows[index];
LinkButton txtLastName = row.FindControl("LinkButton2") as LinkButton;
txtLastName.Text = "editmode";
BindGrid();
// BoundField bb = (BoundField)row.Cells[0].Controls[1];
}
}
}
}
No comments:
Post a Comment