Datagrid + delete funktion
Hej!nu har jeg leget lidt med datagrid og det er jo ganske smart!
Men hvordan får jeg fat i min delete item
C# filen -------------------------------------------------------
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 TestAfTabeller
{
/// <summary>
/// Summary description for CreateWithDatagrid.
/// </summary>
public class CreateWithDatagrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.CheckBox CheckBox1;
protected System.Web.UI.WebControls.CheckBox CheckBox2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
private static ArrayList arl = new ArrayList();
private static int counter = 0;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
this.DataGrid1.DataSource = GetData();
this.DataGrid1.DataBind();
}
}
public ArrayList GetData()
{
return arl;
}
public void BindData()
{
this.DataGrid1.DataSource = GetData();
this.DataGrid1.DataBind();
}
#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.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
bool p = false;
bool f = false;
if(this.CheckBox1.Checked)
p = true;
if(this.CheckBox2.Checked)
f = true;
HoldInfo h = new HoldInfo(this.TextBox1.Text, Convert.ToInt32(this.TextBox2.Text), p, f, counter++);
arl.Add(h);
BindData();
}
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;
BindData();
}
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// string item = e.Item.Cells[1].Text;
HoldInfo h2 = null;
foreach (HoldInfo h in arl)
{
if(h.ID == Convert.ToInt32(item))
{
h2 = h;
break;
}
}
arl.Remove(h2);
BindData();
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.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 length = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
string name = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
foreach (HoldInfo h in arl)
{
if(h.ID == Convert.ToInt32(item))
{
h.Name = name;
h.Lenght = Convert.ToInt32(length);
break;
}
}
DataGrid1.EditItemIndex = -1;
BindData();
}
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindData();
}
}
public class HoldInfo
{
private string _name = "";
private int _lenght = 0;
private bool _pk = false;
private bool _fk = false;
private int _id = 0;
public HoldInfo(string name, int lenght, bool pk, bool fk, int id)
{
_name = name;
_lenght = lenght;
_pk = pk;
_fk = fk;
_id = id;
}
public string Name
{
get{return _name;}
set{_name = value;}
}
public int Lenght
{
get{return _lenght;}
set{_lenght = value;}
}
public bool Primary
{
get{return _pk;}
}
public bool Foreign
{
get{return _fk;}
}
public int ID
{
get{return _id;}
}
}
}
ASPX ----------------------------------------------------------
<%@ Page language="c#" Codebehind="CreateWithDatagrid.aspx.cs" AutoEventWireup="false" Inherits="TestAfTabeller.CreateWithDatagrid" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>CreateWithDatagrid</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" style="Z-INDEX: 100; LEFT: 40px; POSITION: absolute; TOP: 64px" runat="server"></asp:TextBox>
<asp:Label id="Label4" style="Z-INDEX: 109; LEFT: 552px; POSITION: absolute; TOP: 32px" runat="server">Foreign key</asp:Label>
<asp:Label id="Label3" style="Z-INDEX: 108; LEFT: 416px; POSITION: absolute; TOP: 32px" runat="server">Primary key</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 107; LEFT: 224px; POSITION: absolute; TOP: 32px" runat="server">Lenght </asp:Label>
<asp:TextBox id="TextBox2" style="Z-INDEX: 101; LEFT: 224px; POSITION: absolute; TOP: 64px" runat="server"></asp:TextBox>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 56px; POSITION: absolute; TOP: 104px"
runat="server" GridLines="Horizontal" Width="816px">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 680px; POSITION: absolute; TOP: 32px" runat="server"
Text="Button"></asp:Button>
<asp:CheckBox id="CheckBox1" style="Z-INDEX: 104; LEFT: 416px; POSITION: absolute; TOP: 64px"
runat="server"></asp:CheckBox>
<asp:CheckBox id="CheckBox2" style="Z-INDEX: 105; LEFT: 552px; POSITION: absolute; TOP: 64px"
runat="server"></asp:CheckBox>
<asp:Label id="Label1" style="Z-INDEX: 106; LEFT: 40px; POSITION: absolute; TOP: 32px" runat="server">FieldName</asp:Label></form>
</body>
</HTML>
