Thursday, July 26, 2007

GridView Update All Rows At Once









' />




' />




' runat="server" Text='<%# Eval("coprowno") %>' />
' runat="server" Text='<%# Eval("coprowno") %>' />




' runat="server" Text='<%# Eval("cocomtot") %>' />
' runat="server" Text='<%# Eval("cocomtot") %>' />













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;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Text;

public partial class Default6 : System.Web.UI.Page
{
private bool isEditMode = false;
protected bool IsInEditMode
{
get { return this.isEditMode; }
set { this.isEditMode = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
{
isEditMode = true;
BindData();
}
}
private void BindData()
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
string strSQL;
strSQL = "SELECT * FROM [ovowproty]";
//Response.Write(strSQL);
//Response.End();
SqlDataAdapter ad = new SqlDataAdapter(strSQL, myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
MyGridView.DataSource = ds;
//isEditMode = true;
MyGridView.DataBind();


}
private void Update()
{
StringBuilder sb = new StringBuilder();
// build the query
foreach (GridViewRow row in MyGridView.Rows)
{
sb.Append("UPDATE ovowproty SET coprowno = '");
sb.Append((row.FindControl("coprowno") as TextBox).Text);
sb.Append("', ");
sb.Append("cocomtot = '");
sb.Append((row.FindControl("cocomtot") as TextBox).Text);
sb.Append("'");
sb.Append(" WHERE coOwnerID = ");
sb.Append(Convert.ToInt32((row.FindControl("coOwnerID") as Label).Text));
sb.Append(" and coProComTy=");
sb.Append("'");
sb.Append(Convert.ToString((row.FindControl("coProComTy") as Label).Text));
sb.Append("'");
}
// string connectionString = "Server=HCUBE008;Database=School;Trusted_Connection=true";
Response.Write(sb.ToString());
// Response.End();
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand myCommand = new SqlCommand(sb.ToString(), myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}




private string ConnectionString
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
return connectionString;
}
}






protected void Button2_Click(object sender, EventArgs e)
{
Update();
}
}



http://www.svdeals.com/ super value deals





No comments: