Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
5 Answers
- 2 decades agoFavorite Answer
hi ..
C#
step 1-3 - (in HTML):
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<%@ Page Language="c#" CodeBehind="addrows.aspx.cs" AutoEventWireup="false" Inherits="WebApplication6.addrows" %>
<HTML>
<title>Adding Rows</title>
<style>
A { BEHAVIOR: url(..\..\mouseover.htc) }
HR { COLOR: black; HEIGHT: 2px }
.StdText { FONT-WEIGHT: bold; FONT-SIZE: 9pt; FONT-FAMILY: verdana }
.StdTextBox { BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; FONT-SIZE: 9pt; FILTER: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true'); BORDER-LEFT: black 1px solid; BORDER-BOTTOM: black 1px solid; FONT-FAMILY: verdana }
.Shadow { FILTER: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true') }
</style>
<script runat="server">
String [] aTitleOfCourtesy = new String[4] {"Ms.", "Mr.", "Mrs.", "Dr."};
public void Page_Load(Object sender, EventArgs e)
{
// Initialize only the first time...
if (!Page.IsPostBack)
{
lblURL.Text = Request.Url + "<hr>";
}
}
public void OnLoadData(Object sender, EventArgs e)
{
LoadData();
UpdateView();
}
public void AddNewRow(Object sender, EventArgs e)
{
// Grab the dataset
DataSet ds = (DataSet) Session["MyData"];
DataTable dt = ds.Tables["MyTable"];
// Add a blank row
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
// If needed, assign default values
// dr["column_name"] = ...
// Update the in-memory dataset
Session["MyData"] = ds;
// Index of the new item in the page: last +1
int nNewItemIndex = grid.Items.Count;
// If the is full, move to next page. In this case, first item
if (nNewItemIndex >= grid.PageSize)
{
grid.CurrentPageIndex ++;
nNewItemIndex = 0;
}
// Turn edit mode on for the newly added row
grid.EditItemIndex = nNewItemIndex;
// Refresh the grid
UpdateView();
}
public void PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
{
// Set the current item to edit mode
grid.CurrentPageIndex = e.NewPageIndex;
// Refresh the grid
UpdateView();
}
public void ItemCreated(Object sender, DataGridItemEventArgs e)
{
ListItemType lit = e.Item.ItemType;
if (lit == ListItemType.EditItem)
{
// Get the data row
DataRowView drv = (DataRowView) e.Item.DataItem;
// Initializes controls
DropDownList ddTitles = (DropDownList) e.Item.FindControl("ddTitles");
if (drv != null)
ddTitles.SelectedIndex = Array.IndexOf(aTitleOfCourtesy, drv["titleofcourtesy"].ToString());
}
//////////////////////////////////////////////////////////////////////////
if (lit == ListItemType.Pager)
{
// The pager as a whole has the following layout:
//
// <TR><TD colspan=X> ... links ... </TD></TR>
//
// Item points to <TR>. The code below moves to <TD>.
TableCell pager = (TableCell) e.Item.Controls[0];
// Loop through the pager buttons skipping over blanks
// (Blanks are treated as LiteralControl(s)
for (int i=0; i<pager.Controls.Count; i+=2)
{
Object o = pager.Controls[i];
if (o is LinkButton)
{
LinkButton h = (LinkButton) o;
h.Text = "[ " + h.Text + " ]";
}
else
{
Label l = (Label) o;
l.Text = "<b>Page " + l.Text + "</b>";
}
}
}
}
public void EditCommand(Object sender, DataGridCommandEventArgs e)
{
// Set the current item to edit mode
grid.EditItemIndex = e.Item.ItemIndex;
// Refresh the grid
UpdateView();
}
public void UpdateCommand(Object sender, DataGridCommandEventArgs e)
{
// Retrieve the new text from bound columns
int nColPositionIndex = 2;// 0-based position of the column
int nColFromIndex = 3;// 0-based position of the column
TextBox txtPosition = (TextBox) e.Item.Cells[nColPositionIndex].Controls[0];
TextBox txtFrom = (TextBox) e.Item.Cells[nColFromIndex].Controls[0];
// Retrieve the new text in the templated column
TextBox txtFirstName = (TextBox) e.Item.FindControl("txtFirstName");
TextBox txtLastName = (TextBox) e.Item.FindControl("txtLastName");
DropDownList ddTitles = (DropDownList) e.Item.FindControl("ddTitles");
// MUST decide whether to UPDATE or to INSERT. The decision is made based on
// the fact that the DataSet has added rows
DataSet ds = (DataSet) Session["MyData"];
DataTable dt = ds.Tables["MyTable"];
DataRow drLast = dt.Rows[dt.Rows.Count-1];
SqlConnection conn = new SqlConnection(txtConn.Text);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
if (drLast.RowState == DataRowState.Added)
{
// Drop the newly added row from memory
drLast.RejectChanges();
StringBuilder sb1 = new StringBuilder("");
sb1.Append("INSERT Employees (firstname, lastname, titleofcourtesy, title, country) VALUES(");
sb1.Append("@sFirstName, @sLastName, @sTitle, @sPosition, @sCountry)");
cmd.CommandText = sb1.ToString();
// EmployeeID is an identity column
SqlParameter p1 = new SqlParameter("@sPosition", SqlDbType.NVarChar, 30);
p1.Direction = ParameterDirection.Input;
p1.Value = txtPosition.Text;
cmd.Parameters.Add(p1);
SqlParameter p2 = new SqlParameter("@sCountry", SqlDbType.NVarChar, 15);
p2.Direction = ParameterDirection.Input;
p2.Value = txtFrom.Text;
cmd.Parameters.Add(p2);
SqlParameter p3 = new SqlParameter("@sTitle", SqlDbType.NVarChar, 25);
p3.Direction = ParameterDirection.Input;
p3.Value = ddTitles.SelectedItem.Text;
cmd.Parameters.Add(p3);
SqlParameter p4 = new SqlParameter("@sFirstName", SqlDbType.NVarChar, 10);
p4.Direction = ParameterDirection.Input;
p4.Value = txtFirstName.Text;
cmd.Parameters.Add(p4);
SqlParameter p5 = new SqlParameter("@sLastName", SqlDbType.NVarChar, 20);
p5.Direction = ParameterDirection.Input;
p5.Value = txtLastName.Text;
cmd.Parameters.Add(p5);
}
else
{
StringBuilder sb2 = new StringBuilder("");
sb2.Append("UPDATE Employees SET ");
sb2.Append("title=@sPosition, country=@sCountry, titleofcourtesy=@sTitle, ");
sb2.Append("firstname=@sFirstName, lastname=@sLastName ");
sb2.Append("WHERE employeeid=@nEmpID");
cmd.CommandText = sb2.ToString();
SqlParameter p1 = new SqlParameter("@nEmpID", SqlDbType.Int);
p1.Direction = ParameterDirection.Input;
p1.Value = grid.DataKeys[e.Item.ItemIndex];
cmd.Parameters.Add(p1);
SqlParameter p2 = new SqlParameter("@sPosition", SqlDbType.NVarChar, 30);
p2.Direction = ParameterDirection.Input;
p2.Value = txtPosition.Text;
cmd.Parameters.Add(p2);
SqlParameter p3 = new SqlParameter("@sCountry", SqlDbType.NVarChar, 15);
p3.Direction = ParameterDirection.Input;
p3.Value = txtFrom.Text;
cmd.Parameters.Add(p3);
SqlParameter p4 = new SqlParameter("@sTitle", SqlDbType.NVarChar, 25);
p4.Direction = ParameterDirection.Input;
p4.Value = ddTitles.SelectedItem.Text;
cmd.Parameters.Add(p4);
SqlParameter p5 = new SqlParameter("@sFirstName", SqlDbType.NVarChar, 10);
p5.Direction = ParameterDirection.Input;
p5.Value = txtFirstName.Text;
cmd.Parameters.Add(p5);
SqlParameter p6 = new SqlParameter("@sLastName", SqlDbType.NVarChar, 20);
p6.Direction = ParameterDirection.Input;
p6.Value = txtLastName.Text;
cmd.Parameters.Add(p6);
}
// Execute the command
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
// Reset the edit mode for the current item
grid.EditItemIndex = -1;
// Refresh the grid
LoadData();
UpdateView();
}
public void CancelCommand(Object sender, DataGridCommandEventArgs e)
{
// Reset the edit mode for the current item
grid.EditItemIndex = -1;
// Reject changes on the last row
DataSet ds = (DataSet) Session["MyData"];
DataTable dt = ds.Tables["MyTable"];
DataRow drLast = dt.Rows[dt.Rows.Count-1];
if (drLast.RowState == DataRowState.Added)
{
drLast.RejectChanges();
if (grid.Items.Count == 1)
grid.CurrentPageIndex--;
}
// Refresh the grid
UpdateView();
}
////////////////////////////////////////////////////////////////////////
private void LoadData()
{
SqlConnection conn = new SqlConnection(txtConn.Text);
SqlDataAdapter da = new SqlDataAdapter(txtCommand.Text, conn);
DataSet ds = new DataSet();
da.Fill(ds, "MyTable");
Session["MyData"] = ds;
}
private void UpdateView()
{
DataSet ds = (DataSet) Session["MyData"];
// Bind the data
grid.DataSource = ds.Tables["MyTable"];
// Display the data
grid.DataBind();
}
private bool IsLastPage()
{
if (grid.CurrentPageIndex+1 == grid.PageCount)
return true;
return false;
}
</script>
<body bgcolor="ivory" style="FONT-SIZE:small;FONT-FAMILY:arial">
<!-- ASP.NET topbar -->
<h2>Adding New Rows to DataGrids</h2>
<asp:Label runat="server" cssclass="StdText" font-bold="true" id="Label1">Current path: </asp:Label>
<asp:Label runat="server" id="lblURL" cssclass="StdText" style="COLOR:blue"></asp:Label>
<form runat="server">
<table>
<tr>
<td height="23"><asp:label runat="server" text="Connection String" cssclass="StdText" id="Label2" /></td>
<td height="23"><asp:textbox runat="server" id="txtConn" Enabled="false" cssclass="StdTextBox" width="600px"
text="DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=;">DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=123456;</asp:textbox></td>
</tr>
<tr>
<td><asp:label runat="server" text="Command Text" cssclass="StdText" id="Label3" /></td>
<td><asp:textbox runat="server" id="txtCommand" Enabled="false" width="600px" cssclass="StdTextBox"
text="SELECT employeeid, titleofcourtesy, firstname, lastname, title, country FROM Employees" /></td>
</tr>
</table>
<br>
<asp:linkbutton runat="server" text="Go get data..." onclick="OnLoadData" id="Linkbutton1" />
<hr>
<asp:DataGrid id="grid" runat="server" AutoGenerateColumns="false" CssClass="Shadow" BackColor="white"
CellPadding="2" CellSpacing="0" BorderStyle="solid" BorderColor="black" BorderWidth="1" Font-Size="x-small"
Font-Names="verdana" ShowFooter="true" AllowPaging="true" PageSize="4" DataKeyField="employeeid"
OnItemCreated="ItemCreated" OnPageIndexChanged="PageIndexChanged" OnEditCommand="EditCommand"
OnUpdateCommand="UpdateCommand" OnCancelCommand="CancelCommand">
<AlternatingItemStyle BackColor="palegoldenrod" />
<ItemStyle BackColor="beige" />
<PagerStyle Mode="NumericPages" HorizontalAlign="right" />
<EditItemStyle BackColor="yellow" Font-Bold="true" />
<HeaderStyle ForeColor="white" BackColor="brown" HorizontalAlign="center" Font-Bold="true" />
<columns>
<asp:BoundColumn runat="server" DataField="employeeid" HeaderText="ID" Readonly="true" DataFormatString="<span style='margin-left:5;margin-right:5'>{0}</span>">
<itemstyle backcolor="lightblue" font-bold="true" HorizontalAlign="right" />
</asp:BoundColumn>
<asp:TemplateColumn runat="server" HeaderText="Employee Name">
<itemtemplate>
<asp:label runat="server" style="margin-left:5;margin-right:5" Text='<%# DataBinder.Eval(Container.DataItem, "TitleOfCourtesy") + "<b> " +
DataBinder.Eval(Container.DataItem, "LastName") + "</b>" + ", " +
DataBinder.Eval(Container.DataItem, "FirstName") %>' />
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist runat="server" id="ddTitles" DataSource='<% # aTitleOfCourtesy %>' />
<asp:textbox runat="server" width="80px" id="txtFirstName" tabindex="0" Text='<%# DataBinder.Eval(Container.DataItem, "firstname") %>' /><br>
<asp:textbox runat="server" width="140px" id="txtLastName" Text='<%# DataBinder.Eval(Container.DataItem, "lastname") %>' />
</edititemtemplate>
<footertemplate>
<asp:linkbutton runat="server" id="btnNewRow" onclick="AddNewRow" Enabled='<%# IsLastPage() %>' Text="Add new row..." />
</footertemplate>
</asp:TemplateColumn>
<asp:BoundColumn runat="server" DataField="title" HeaderText="Position" />
<asp:BoundColumn runat="server" DataField="country" HeaderText="From" />
<asp:EditCommandColumn runat="server" EditText="<img src=edit.gif border=0 align=absmiddle alt='Edit this item'>"
UpdateText="<img src=ok.gif border=0 align=absmiddle alt='Save changes'>" CancelText="<img src=cancel.gif border=0 align=absmiddle alt='Cancel editing'>">
<itemstyle BackColor="yellow" HorizontalAlign="center" />
</asp:EditCommandColumn>
</columns>
</asp:DataGrid>
</form>
</body>
</HTML>
----------------------------------------------------
step 2-3 - (in Code):
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication6
{
/// <summary>
/// Summary description for addrows.
/// </summary>
public class addrows : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label lblURL;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox txtConn;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox txtCommand;
protected System.Web.UI.WebControls.LinkButton Linkbutton1;
protected System.Web.UI.WebControls.DataGrid grid;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Linkbutton1.Click += new System.EventHandler(this.Linkbutton1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Linkbutton1_Click(object sender, System.EventArgs e)
{
}
}
}
----------------------------------------------------
step 3-3 :
you need images for 'OK' , 'Cancel' and 'Edit'
- Anonymous4 years ago
gasoline......no longer. concert activities no dought approximately it. stay overall performance tics front row are like 250-750 each plus gasoline isnt that undesirable(a million.22 /L), it would be like 3 hundred in gasoline for 3 months, i ought to easily sell one value ticket and pay for the gasoline
- How do you think about the answers? You can sign in to vote the answer.
- Anonymous2 decades ago
thinking.....