Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

Hi!

In the following code snippet, I am trying to SELECT/EDIT/UPDATE/CANCEL/DELETE GridView row. But I am not able to UPDATE, please help!



Code snippet (Default.aspx.cs):



using System;

using System.Data;

using System.Configuration;

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;

using System.Data.SqlClient;

using System.Collections;

public partial class _Default : System.Web.UI.Page

{

DataTable dt;

SqlConnection connection = new SqlConnection("Server=(local);User ID=sa;password=zxcvbnm;Initial Catalog=master");

public DataTable datatable()

{

SqlCommand command = new SqlCommand("SELECT * FROM customer", connection);

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = command;

DataTable dt = new DataTable();

da.Fill(dt);

return dt;

}

public class MyTableRow

{

public int cid;

public string fnme;

public string lnme;

public string add;

public string phn;

public string mail;

}

public class MyTable

{

public System.Collections.ArrayList arraylist = new ArrayList(20);

}

public MyTable mt = new MyTable();

private void FillData()

{

dt = new DataTable();

SqlCommand command = new SqlCommand("SELECT * FROM customer", connection);

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = command;

da.Fill(dt);

}

private void BindData()

{

GridView1.DataSource = dt;

GridView1.DataBind();

}

protected void Page_Load(object sender, EventArgs e)

{

FillData();

BindData();

TableRow trow1 = new TableRow();

TableCell tcell1 = new TableCell();

TableCell tcell2 = new TableCell();

TableCell tcell3 = new TableCell();

TableCell tcell4 = new TableCell();

TableCell tcell5 = new TableCell();

TableCell tcell6 = new TableCell();

tcell1.Text = "custid";

tcell2.Text = "fname";

tcell3.Text = "lname";

tcell4.Text = "address";

tcell5.Text = "phone";

tcell6.Text = "email";

trow1.Cells.Add(tcell1);

trow1.Cells.Add(tcell2);

trow1.Cells.Add(tcell3);

trow1.Cells.Add(tcell4);

trow1.Cells.Add(tcell5);

trow1.Cells.Add(tcell6);

Table1.Rows.Add(trow1);

if (!IsPostBack)

{

int n = mt.arraylist.Count; for (int i = 0; i < n; i++)

{

TableRow trow2 = new TableRow();

MyTableRow mtrow = new MyTableRow();

mtrow = (MyTableRow)mt.arraylist[i];

TableCell tcell7 = new TableCell();

TableCell tcell8 = new TableCell();

TableCell tcell9 = new TableCell();

TableCell tcell10 = new TableCell();

TableCell tcell11 = new TableCell();

TableCell tcell12 = new TableCell();

tcell7.Text = mtrow.cid.ToString();

tcell8.Text = mtrow.fnme;

tcell9.Text = mtrow.lnme;

tcell10.Text = mtrow.add;

tcell11.Text = mtrow.phn;

tcell12.Text = mtrow.mail;

trow2.Cells.Add(tcell7);

trow2.Cells.Add(tcell8);

trow2.Cells.Add(tcell9);

trow2.Cells.Add(tcell10);

trow2.Cells.Add(tcell11);

trow2.Cells.Add(tcell12);

Table1.Rows.Add(trow2);

}

}

}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

{

FillData();

BindData();

int index = GridView1.SelectedIndex;

string ci = datatable().Rows[index][0].ToString();

string fn = datatable().Rows[index][1].ToString();

string ln = datatable().Rows[index][2].ToString();

string ad = datatable().Rows[index][3].ToString();

string ph = datatable().Rows[index][4].ToString();

string em = datatable().Rows[index][5].ToString();

MyTableRow mtrow1 = new MyTableRow();

MyTableRow mtrow2 = new MyTableRow();

mtrow1.cid = int.Parse(ci);

mtrow1.fnme = fn;

mtrow1.lnme = ln;

mtrow1.add = ad;

mtrow1.phn = ph;

mtrow1.mail = em;

int n = mt.arraylist.Count;

mt.arraylist.Add(mtrow1);

n = mt.arraylist.Count;

for (int i = 0; i < n; i++)

{

TableRow trow = new TableRow();

mtrow1 = (MyTableRow)mt.arraylist[i];

TableCell c1 = new TableCell();

TableCell c2 = new TableCell();

TableCell c3 = new TableCell();

TableCell c4 = new TableCell();

TableCell c5 = new TableCell();

TableCell c6 = new TableCell();

c1.Text = mtrow1.cid.ToString();

c2.Text = mtrow1.fnme;

c3.Text = mtrow1.lnme;

c4.Text = mtrow1.add;

c5.Text = mtrow1.phn;

c6.Text = mtrow1.mail;

trow.Cells.Add(c1);

trow.Cells.Add(c2);

trow.Cells.Add(c3);

trow.Cells.Add(c4);

trow.Cells.Add(c5);

trow.Cells.Add(c6);

Table1.Rows.Add(trow);

}

}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

int cid = int.Parse(datatable().Rows[e.RowIndex][0].ToString());

string delete = "DELETE FROM customer WHERE custid = '" + cid + "';";

SqlCommand command = new SqlCommand(delete, connection);

connection.Open();

command.ExecuteNonQuery();

connection.Close();

Page.Response.Redirect(Page.Request.Url.ToString(), true);

}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{

GridView1.EditIndex = e.NewEditIndex;

FillData();

BindData();

}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

int cid = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);

string fn = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;

string ln = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;

string ad = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text;

string ph = ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text;

string em = ((TextBox)GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text;

string update = "UPDATE customer SET fname = '" + fn +

"', lname = '" + ln +

"', address = '" + ad +

"', phone = '" + ph +

"', email = '" + em +

"' WHERE custid = '" + cid +

"';";

SqlCommand command = new SqlCommand(update, connection);

connection.Open();

command.ExecuteNonQuery();

connection.Close();

GridView1.EditIndex = -1;

FillData();

BindData();

Page.Response.Redirect(Page.Request.Url.ToString(), true);

}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

{

GridView1.EditIndex = -1;

FillData();

BindData();

Page.Response.Redirect(Page.Request.Url.ToString(), true);

}

}



Code snippet (Default.aspx):

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

<!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">

<div>

<asp:GridView ID="GridView1" runat="server" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">

<Columns>

<asp:CommandField ShowSelectButton="True" />

<asp:CommandField ShowDeleteButton="True" />

<asp:CommandField ShowEditButton="True" />

</Columns>

</asp:GridView>



<br />

<br />

<br />

<asp:Table ID="Table1" runat="server" Height="104px" Width="231px">

</asp:Table>

</div>

</form>

</body>

</html>



Code snippet (Database Table):

USE master

IF OBJECT_ID('dbo.customer', 'U') IS NOT NULL

DROP TABLE dbo.customer

CREATE TABLE dbo.customer

(

custid int identity(1,1) NOT NULL,

fname varchar(50),

lname varchar(50),

address varchar(50),

phone varchar(50),

email varchar(50),

CONSTRAINT PK_customer PRIMARY KEY(custid)

);

GO

INSERT INTO customer(fname, lname, address, phone, email)

VALUES('Bill', 'Gates', 'Medina, Washington, USA', '001202', 'billgates@microsoft.com');

GO

INSERT INTO customer(fname, lname, address, phone, email)

VALUES('George', 'Bush', 'Washington D.C.,USA', '001202', 'georgebush@usa.com');

GO

INSERT INTO customer(fname, lname, address, phone, email)

VALUES('Gordon', 'Brown', 'London, UK', '0044020', 'gordonbrown@uk.com');

GO

INSERT INTO customer(fname, lname, address, phone, email)

VALUES('Yasuo', '***uda', 'Tokyo, Japan', '0081003', 'yasuo***uda@japan.com');

GO

INSERT INTO customer(fname, lname, address, phone, email)

VALUES('Angela', 'Merkel', 'Berlin, Germany', '0049030', 'angelamerkel@germany.com');

GO

INSERT INTO customer(fname, lname, address, phone, email)

VALUES('Nicolas', 'Sarkozy', 'Paris, France', '0033001', 'jacqueschirac@france.com');

GO

INSERT INTO customer(fname, lname, address, phone, email)

VALUES('Stephen', 'Harper', 'Ottawa, Canada', '001613', 'stephenharper@canada.com');

GO

INSERT INTO customer(fname, lname, address, phone, email)

VALUES('Romano', 'Prodi', 'Rome, Italy', '0039006', 'romanoprodi@italy.com');

GO

INSERT INTO customer(fname, lname, address, phone, email)

VALUES('Vladimir', 'Putin', 'Moscow, Russia', '007095', 'vladimirputin@russia.com');

GO

INSERT INTO customer(fname, lname, address, phone, email)

VALUES('Ban', 'Ki-moon', 'New York, USA', '001718', 'banki-moon@un.com');

GO

SELECT * FROM customer
[11844 byte] By [zippo] at [2007-11-11 11:59:39]
# 1 Re: GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
Can you use the UpdateCommand and DeleteCommand of the SqlDataSource (just like SelectCommand - you specify the SQL statement).

Then set the GridView AutoGenerateEditButton, AutoGenerateSelectButton and AutoGenerateDeleteButton to true
mdb002 at 2007-11-11 23:10:44 >
# 2 Re: GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
I have given you the complete code (including for database). Please run and see!
zippo at 2007-11-11 23:11:38 >