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.