对数据进行更新时,从后台取gridview绑定的值,怎么做
protectedvoidgd_RowUpdating(objectsender,GridViewUpdateEventArgse){intid=Convert.ToIn...
protected void gd_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(gd.DataKeys[e.RowIndex].Value.ToString().Trim());
Admin admin = new Admin();
admin.id = Convert.ToInt32(((TextBox)gd.Rows[e.RowIndex].FindControl("Id")).ToString().Trim());//报错,未将对象引用设置到对象的实例
admin.name = ((TextBox)gd.Rows[e.RowIndex].FindControl("Name")).ToString().Trim();//报错
admin.password = ((TextBox)gd.Rows[e.RowIndex].FindControl("PassWord")).Text.Trim();//报错
admin.adminlevel = Convert.ToInt32(((TextBox)gd.Rows[e.RowIndex].FindControl("AdminLevel")).Text.Trim());//报错
admin.passwordMD5 = ((TextBox)gd.Rows[e.RowIndex].FindControl("PassWordMD5")).Text.Trim();//报错
}
整了很久,还是不懂怎么改,求助ing 展开
{
int id = Convert.ToInt32(gd.DataKeys[e.RowIndex].Value.ToString().Trim());
Admin admin = new Admin();
admin.id = Convert.ToInt32(((TextBox)gd.Rows[e.RowIndex].FindControl("Id")).ToString().Trim());//报错,未将对象引用设置到对象的实例
admin.name = ((TextBox)gd.Rows[e.RowIndex].FindControl("Name")).ToString().Trim();//报错
admin.password = ((TextBox)gd.Rows[e.RowIndex].FindControl("PassWord")).Text.Trim();//报错
admin.adminlevel = Convert.ToInt32(((TextBox)gd.Rows[e.RowIndex].FindControl("AdminLevel")).Text.Trim());//报错
admin.passwordMD5 = ((TextBox)gd.Rows[e.RowIndex].FindControl("PassWordMD5")).Text.Trim();//报错
}
整了很久,还是不懂怎么改,求助ing 展开
2个回答
展开全部
给你个更新的列子:
前台 <asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="5"
AutoGenerateColumns="False" HeaderStyle-VerticalAlign="Middle" CellPadding="3"
Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
Height="221px" Width="500px" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" >
<Columns>
<asp:BoundField DataField="CID" HeaderText="用户ID" ReadOnly="true">
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="用户姓名" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Sex" HeaderText="性别" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="家庭住址" >
<ItemStyle Width="140px" />
</asp:BoundField>
<asp:BoundField DataField="Post" HeaderText="邮政编码" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:CommandField HeaderText="编辑" ShowEditButton="True">
<ItemStyle Width="70px" />
</asp:CommandField>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<PagerSettings Visible="False" />
<FooterStyle Font-Bold="True" />
<HeaderStyle Font-Bold="False" Font-Italic="False" />
</asp:GridView>
后台: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;
public partial class Update : System.Web.UI.Page
{
SqlConnection sqlcon;
string strCon = "Data Source=(local);Database=wxd;Uid=sa;Pwd=sa";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
//绑定GridView
public void bind()
{
string sqlstr = "select * from Admin";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "Admin");
GridView1.DataSource = myds;
GridView1.DataBind();
}
//编辑行
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
bind();
}
//更新行
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string strsql = "update Admin set Name =@Name ,Sex =@Sex, Address =@Address,Post =@Post where CID = @CID ";
SqlCommand sqlcmd = new SqlCommand(strsql, sqlcon);
try
{
sqlcmd.Parameters.Add(new SqlParameter("@CID", SqlDbType.Int, 4));
sqlcmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar,50 ));
sqlcmd.Parameters.Add(new SqlParameter("@Sex", SqlDbType.VarChar, 50));
sqlcmd.Parameters.Add(new SqlParameter("@Address", SqlDbType.VarChar, 50));
sqlcmd.Parameters.Add(new SqlParameter("@Post", SqlDbType.VarChar, 50));
sqlcmd.Parameters["@CID"].Value = GridView1.Rows[e.RowIndex].Cells[0].Text;
sqlcmd.Parameters["@Name"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
sqlcmd.Parameters["@Sex"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
sqlcmd.Parameters["@Address"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
sqlcmd.Parameters["@Post"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
sqlcmd.Connection.Open();
sqlcmd.ExecuteNonQuery();
this.GridView1.EditIndex = -1;
}
catch (SqlException ex)
{
throw ex;
}
sqlcmd.Connection.Close();
bind();
}
//取消编辑行
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
bind();
}
//设定列宽
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate) || e.Row.RowState == DataControlRowState.Edit)
{
for (int i = 1; i < GridView1.Columns.Count-1;i++ )
{
TextBox txt = (TextBox)e.Row.Cells[i].Controls[0];
txt.Width = Unit.Pixel(60);
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C9D3E2',this.style.fontWeight='';");
//当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
e.Row.Attributes["style"] = "Cursor:pointer";
}
}
}
前台 <asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="5"
AutoGenerateColumns="False" HeaderStyle-VerticalAlign="Middle" CellPadding="3"
Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
Height="221px" Width="500px" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" >
<Columns>
<asp:BoundField DataField="CID" HeaderText="用户ID" ReadOnly="true">
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="用户姓名" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Sex" HeaderText="性别" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="家庭住址" >
<ItemStyle Width="140px" />
</asp:BoundField>
<asp:BoundField DataField="Post" HeaderText="邮政编码" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:CommandField HeaderText="编辑" ShowEditButton="True">
<ItemStyle Width="70px" />
</asp:CommandField>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<PagerSettings Visible="False" />
<FooterStyle Font-Bold="True" />
<HeaderStyle Font-Bold="False" Font-Italic="False" />
</asp:GridView>
后台: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;
public partial class Update : System.Web.UI.Page
{
SqlConnection sqlcon;
string strCon = "Data Source=(local);Database=wxd;Uid=sa;Pwd=sa";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
//绑定GridView
public void bind()
{
string sqlstr = "select * from Admin";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "Admin");
GridView1.DataSource = myds;
GridView1.DataBind();
}
//编辑行
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
bind();
}
//更新行
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string strsql = "update Admin set Name =@Name ,Sex =@Sex, Address =@Address,Post =@Post where CID = @CID ";
SqlCommand sqlcmd = new SqlCommand(strsql, sqlcon);
try
{
sqlcmd.Parameters.Add(new SqlParameter("@CID", SqlDbType.Int, 4));
sqlcmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar,50 ));
sqlcmd.Parameters.Add(new SqlParameter("@Sex", SqlDbType.VarChar, 50));
sqlcmd.Parameters.Add(new SqlParameter("@Address", SqlDbType.VarChar, 50));
sqlcmd.Parameters.Add(new SqlParameter("@Post", SqlDbType.VarChar, 50));
sqlcmd.Parameters["@CID"].Value = GridView1.Rows[e.RowIndex].Cells[0].Text;
sqlcmd.Parameters["@Name"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
sqlcmd.Parameters["@Sex"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
sqlcmd.Parameters["@Address"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
sqlcmd.Parameters["@Post"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
sqlcmd.Connection.Open();
sqlcmd.ExecuteNonQuery();
this.GridView1.EditIndex = -1;
}
catch (SqlException ex)
{
throw ex;
}
sqlcmd.Connection.Close();
bind();
}
//取消编辑行
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
bind();
}
//设定列宽
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate) || e.Row.RowState == DataControlRowState.Edit)
{
for (int i = 1; i < GridView1.Columns.Count-1;i++ )
{
TextBox txt = (TextBox)e.Row.Cells[i].Controls[0];
txt.Width = Unit.Pixel(60);
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C9D3E2',this.style.fontWeight='';");
//当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
e.Row.Attributes["style"] = "Cursor:pointer";
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询