M
mohaaron
This seems like it should be simple to do but for some reason I have
been unable to make it work.
I would like to databind a SqlDataSource to a GridView during the
click event of a button. This sounds easy but the next requirement is
that the GridView is editable. So I have included the SelectCommand
and the UpdateCommand on the SqlDataSource to allow the GridView to be
editable. I have now been able to get the GridView to display data on
the button click using the following code. As you can see I've tried
three different ways of doing this and each causes a different
problem. Now I am stuck and could use some help.
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 EditableGridView2 : System.Web.UI.Page
{
// 3. Displays data but the row edit button causes the data to
dissapear.
protected SqlDataSource dataSource = new SqlDataSource();
protected void Page_Load(object sender, EventArgs e)
{
this.gridView.RowEditing += new
GridViewEditEventHandler(gridView_RowEditing);
this.gridView.RowUpdating += new
GridViewUpdateEventHandler(gridView_RowUpdating);
this.gridView.RowUpdated += new
GridViewUpdatedEventHandler(gridView_RowUpdated);
this.getData.Click += new EventHandler(getData_Click);
// 3. Displays data but the row edit button causes the data to
dissapear.
dataSource.ID = "SqlDataSource1";
dataSource.ConnectionString =
ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ToString();
this.Controls.Add(dataSource);
this.gridView.DataSourceID = dataSource.ID;
}
void getData_Click(object sender, EventArgs e)
{
// 1. and 2.
//SqlDataSource dataSource = new SqlDataSource();
//dataSource.ID = "SqlDataSource1";
//dataSource.ConnectionString =
ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ToString();
dataSource.SelectCommand = "SELECT [appID], [status], [clientName]
FROM [Applications] WHERE ([status] = @status)";
dataSource.UpdateCommand = "UPDATE [MemberApplications] SET [status]
= @status, [clientName] = @clientName WHERE [appID] = @appID";
dataSource.SelectParameters.Add("status", "Pending");
dataSource.UpdateParameters.Add(new Parameter("status"));
dataSource.UpdateParameters.Add(new Parameter("clientName"));
dataSource.UpdateParameters.Add(new Parameter("appID"));
// 1. Displays data but clicking the row edit causes the exception
"The DataSourceID of 'gridView' must be the ID of a control of type
IDataSource. A control with ID 'SqlDataSource1' could not be found."
//this.Controls.Add(dataSource);
//this.gridView.DataSourceID = dataSource.ID;
// 2. Displays data but the row edit button does not work.
//this.gridView.DataSource = dataSource;
//this.gridView.DataBind();
}
void gridView_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
this.message.Text = e.AffectedRows.ToString();
}
void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//
}
void gridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//
}
}
been unable to make it work.
I would like to databind a SqlDataSource to a GridView during the
click event of a button. This sounds easy but the next requirement is
that the GridView is editable. So I have included the SelectCommand
and the UpdateCommand on the SqlDataSource to allow the GridView to be
editable. I have now been able to get the GridView to display data on
the button click using the following code. As you can see I've tried
three different ways of doing this and each causes a different
problem. Now I am stuck and could use some help.
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 EditableGridView2 : System.Web.UI.Page
{
// 3. Displays data but the row edit button causes the data to
dissapear.
protected SqlDataSource dataSource = new SqlDataSource();
protected void Page_Load(object sender, EventArgs e)
{
this.gridView.RowEditing += new
GridViewEditEventHandler(gridView_RowEditing);
this.gridView.RowUpdating += new
GridViewUpdateEventHandler(gridView_RowUpdating);
this.gridView.RowUpdated += new
GridViewUpdatedEventHandler(gridView_RowUpdated);
this.getData.Click += new EventHandler(getData_Click);
// 3. Displays data but the row edit button causes the data to
dissapear.
dataSource.ID = "SqlDataSource1";
dataSource.ConnectionString =
ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ToString();
this.Controls.Add(dataSource);
this.gridView.DataSourceID = dataSource.ID;
}
void getData_Click(object sender, EventArgs e)
{
// 1. and 2.
//SqlDataSource dataSource = new SqlDataSource();
//dataSource.ID = "SqlDataSource1";
//dataSource.ConnectionString =
ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ToString();
dataSource.SelectCommand = "SELECT [appID], [status], [clientName]
FROM [Applications] WHERE ([status] = @status)";
dataSource.UpdateCommand = "UPDATE [MemberApplications] SET [status]
= @status, [clientName] = @clientName WHERE [appID] = @appID";
dataSource.SelectParameters.Add("status", "Pending");
dataSource.UpdateParameters.Add(new Parameter("status"));
dataSource.UpdateParameters.Add(new Parameter("clientName"));
dataSource.UpdateParameters.Add(new Parameter("appID"));
// 1. Displays data but clicking the row edit causes the exception
"The DataSourceID of 'gridView' must be the ID of a control of type
IDataSource. A control with ID 'SqlDataSource1' could not be found."
//this.Controls.Add(dataSource);
//this.gridView.DataSourceID = dataSource.ID;
// 2. Displays data but the row edit button does not work.
//this.gridView.DataSource = dataSource;
//this.gridView.DataBind();
}
void gridView_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
this.message.Text = e.AffectedRows.ToString();
}
void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//
}
void gridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//
}
}