DataGrid: esempio. TestDataGridConnection.aspx.cs

FAQ di programmazione in .NET

DataGrid: esempio. TestDataGridConnection.aspx.cs

Messaggioda alesslud » ven apr 06, 2007 2:19 pm

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

public partial class abruzzoparchi_testconnection_TestDataGridConnection : System.Web.UI.Page
{

DataTable cart;
DataView cartView;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["DG6_ShoppingCart"] == null)
{
cart = new DataTable();
//aggiungo l'intestazione di ogni singola colonna
cart.Columns.Add(new DataColumn("Qty", typeof(string)));
cart.Columns.Add(new DataColumn("Item", typeof(string)));
cart.Columns.Add(new DataColumn("Price", typeof(string)));
//Metto in sessione il data table
Session["DG6_ShoppingCart"] = cart;

// first load -- prepopulate with some data
for (int i = 1; i < 5; i++)
{
//aggiungo righe al datagrid nel caso in cui non lo
//abbia in sessione
DataRow dr = cart.NewRow();
dr[0] = ((int)(i % 2) + 1).ToString();
dr[1] = "Item " + i.ToString();
dr[2] = ((double)(1.23 * (i + 1))).ToString();
cart.Rows.Add(dr);
}
}
else
{
//altrimenti mi carico dalla sessione il datagrid
cart = (DataTable)Session["DG6_ShoppingCart"];
}

cartView = new DataView(cart);
cartView.Sort = "Item";
if (!IsPostBack)
{
this.BindGrid();
}
}

//utilizzo il seguente metodo per eseguire aggiornamenti del
//datagrid
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e)
{
MyDataGrid.EditItemIndex = (int)e.Item.ItemIndex;
this.BindGrid();
}

//elimina una riga dal datagrid
public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e)
{
MyDataGrid.EditItemIndex = -1;
this.BindGrid();
}

//esegue l'aggiornamento del datagrid
public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)
{
// For bound columns the edited value is stored in a textbox,
// and the textbox is the 0th element in the column's cell
string item = e.Item.Cells[1].Text;
string qty = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
string price = ((TextBox)e.Item.Cells[3].Controls[0]).Text;

// with a database, we'd use an update command. Since we're using an in-memory
// DataTable, we'll delete the old row and replace it with a new one
//remove old entry
cartView.RowFilter = "Item='" + item + "'";
if (cartView.Count > 0)
{
//item exists in cart
cartView.Delete(0);
}
cartView.RowFilter = "";

//add new entry
DataRow dr = cart.NewRow();
dr[0] = qty;
dr[1] = item;
dr[2] = price;
cart.Rows.Add(dr);

MyDataGrid.EditItemIndex = -1;
this.BindGrid();
}
//riempie il datagrid
public void BindGrid()
{
MyDataGrid.DataSource = cartView;
MyDataGrid.DataBind();
}
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

.aspx che visualizza il DataGrid: TestDataGridConnection

Messaggioda alesslud » ven apr 06, 2007 2:21 pm

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestDataGridConnection.aspx.cs" Inherits="abruzzoparchi_testconnection_TestDataGridConnection" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="Form1" runat="server">

<h3><font face="Verdana">Using an Edit Command Column in DataGrid</font></h3>

<asp:DataGrid id="MyDataGrid" runat="server"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
OnEditCommand="MyDataGrid_Edit"
OnCancelCommand="MyDataGrid_Cancel"
OnUpdateCommand="MyDataGrid_Update"
AutoGenerateColumns="false"
>

<Columns>
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
ItemStyle-Wrap="false"
HeaderText="Edit Command Column"
HeaderStyle-Wrap="false"
/>
<asp:BoundColumn HeaderText="Item" ReadOnly="true" DataField="Item"/>
<asp:BoundColumn HeaderText="Quantity" DataField="Qty"/>
<asp:BoundColumn HeaderText="Price" DataField="Price"/>
</Columns>
</asp:DataGrid>

</form>
</body>
</html>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am


Torna a .NET

Chi c’è in linea

Visitano il forum: Nessuno e 1 ospite

cron