Edit Gridview using stored procesure

M

Madison

Hi all,
I'm try to write the code for gridview to delete, edit (only one or two
columns), page, sort using stored procedure to get dataTable to bind data
(avoid using SQLDataSource to bind data). All the samples I see always use
SQLDataSource which in the real business is not the good ideas.

Any ideas or samples would be appreciated.

Thank you.
 
W

Walter Wang [MSFT]

Hi Madison,

First, a SqlDataSource is also able to use stored procedure to
select/insert/delete/update data.

Please note that if you're not using a DataSource control to bind to
GridView, you will need to handle many events yourself such as
OnRowUpdating, OnSorting.

To do it all yourself, you can refer to following pages:

#Sorting GridView Manually
http://www.gridviewguy.com/ArticleDetails.aspx?articleID=176

#GridView Custom Paging
http://www.gridviewguy.com/ArticleDetails.aspx?articleID=210


And here's an example on how to edit/delete rows in GridView when bound to
a DataTable:


<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}

private void BindGrid()
{
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}

protected DataTable GetDataSource()
{
const string key = "MyDataSource";
DataTable dt = Session[key] as DataTable;
if (dt == null)
{
dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(1, "first object");
dt.Rows.Add(2, "second object");
Session[key] = dt;
}
return dt;
}

protected void GridView1_RowEditing(object sender,
GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
TextBox txtName =
GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtName") as TextBox;
string newname = txtName.Text;

FindRowByID(id)["Name"] = newname;
GridView1.EditIndex = -1;
BindGrid();
}

private DataRow FindRowByID(int id)
{
DataRow[] rows = GetDataSource().Select("ID = " + id.ToString());
return rows[0];
}

protected void GridView1_RowDeleting(object sender,
GridViewDeleteEventArgs e)
{
int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
FindRowByID(id).Delete();
BindGrid();
}
</script>

<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 DataKeyNames="ID" AutoGenerateColumns="false"
ID="GridView1" runat="server"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:CommandField ShowEditButton="true"
ShowDeleteButton="true" />
<asp:BoundField HeaderText="ID" DataField="ID"
ReadOnly="true" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server"
Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server"
Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>



Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Madison

Hi Walter,

Thank you for your reply. I will try your coding and let you know how is
going.

Madison
 
M

Madison

Hi Walter,

Thanks, the codes work very good.

I'm interest in using SqlDataSource with stored procedure can you refer to
web site that give more information? Do you have any pro and con on it?

Madison.
 
W

Walter Wang [MSFT]

Hi Madison,

When you use the wizard to configure SqlDataSource, in the step "How would
you like to retrieve data from your database", you can select "Specify a
custom SQL statement or stored procedure"; when you click next, you can
choose a stored procedure for the SELECT/UPDATE/INSERT/DELETE.

Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

JimS

Hello Walter, and or anyone else!
How would you update the dataset if one of the fields in the grid was a
template item? The case I am looking to see is one item in a datagrid that
has been converted to a dropdown box. The dropdown box value is what I
would like to show up in the edited record. The example is a drop down
box of states. When the correct state is chosen I would like to update the
record with that value.

Thanks

Hi Madison,

First, a SqlDataSource is also able to use stored procedure to
select/insert/delete/update data.

Please note that if you're not using a DataSource control to bind to
GridView, you will need to handle many events yourself such as
OnRowUpdating, OnSorting.

To do it all yourself, you can refer to following pages:

#Sorting GridView Manually
http://www.gridviewguy.com/ArticleDetails.aspx?articleID=176

#GridView Custom Paging
http://www.gridviewguy.com/ArticleDetails.aspx?articleID=210


And here's an example on how to edit/delete rows in GridView when bound to
a DataTable:


<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}

private void BindGrid()
{
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}

protected DataTable GetDataSource()
{
const string key = "MyDataSource";
DataTable dt = Session[key] as DataTable;
if (dt == null)
{
dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(1, "first object");
dt.Rows.Add(2, "second object");
Session[key] = dt;
}
return dt;
}

protected void GridView1_RowEditing(object sender,
GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
TextBox txtName =
GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtName") as TextBox;
string newname = txtName.Text;

FindRowByID(id)["Name"] = newname;
GridView1.EditIndex = -1;
BindGrid();
}

private DataRow FindRowByID(int id)
{
DataRow[] rows = GetDataSource().Select("ID = " + id.ToString());
return rows[0];
}

protected void GridView1_RowDeleting(object sender,
GridViewDeleteEventArgs e)
{
int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
FindRowByID(id).Delete();
BindGrid();
}
</script>

<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 DataKeyNames="ID" AutoGenerateColumns="false"
ID="GridView1" runat="server"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:CommandField ShowEditButton="true"
ShowDeleteButton="true" />
<asp:BoundField HeaderText="ID" DataField="ID"
ReadOnly="true" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server"
Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server"
Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>



Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

For a DropDownList, all you need to change in the demo code is to cast the
control returned from FindControl to a DropDownList and you should be able
to get the correct value selected the user currently.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Matt

For a DropDownList, all you need to change in the demo code is to cast the
control returned from FindControl to a DropDownList and you should be able
to get the correct value selected the user currently.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Or, if you are using a SqlDataSource, as long as the parameter in the
Update command is the same as the field name (ie State = @State), it
should work without any hassle. also make sure you have the
DataKeyName set in your grid (it should be the primary key).
 
W

Walter Wang [MSFT]

Hi Madison,

Since I haven't seen your reply yet, I'm writing to check the status of
this post. Thanks.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top