asp GridView1里面怎么取值啊 就是更新之后 要不值写入数据库

asp GridView1里面怎么取值啊 就是更新之后 要不值写入数据库,第1张

在GridView1_RowUpdating这个事件里写代码 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

int TypeId = intParse(GridView1Rows[eRowIndex]Cells[0]Text);//id编号

string TypeName = ((TextBox)(GridView1Rows[eRowIndex]Cells[1]Controls[0]))Text;第二列的数据

string sql = "update brand set name = '" +

TypeName + "' where id = " + TypeId;//修改语句

AccessExec(sql);//我写的一个修改方法,你应该能解决

GridView1EditIndex = -1;

GridView1DataSource = AccessSearch("select from brand");//重新绑定

GridView1DataBind();

}不懂的地方可以问

Pro C# 2008 and the NET 35 Platform >

首先你gridview绑定的数据源最好是DataTable类型,当你编辑好数据进行修改后,可以将datagridview的数据源转换成DataTable类型,然后利用 DataRowState循环获取每行的状态是更改还是更新 *** 作,如果为更新 *** 作你可以遍历每列生成update的语句,然后统一抛给执行更新sql的方法去 *** 作。源码给你吧

DataTable dt1 = (dataGridView1DataSource as DataTable)GetChanges();

if (dt1 != null)

{

for (int i = 0, h = dt1RowsCount; i < h; i++)

{

DataRowState rowState = new DataRowState();

rowState = dt1Rows[i]RowState;

string state = rowStateToString();

string id = GuidNewGuid()ToString();

if (stateEquals("added", StringComparisonCurrentCultureIgnoreCase))

{

string str = stringFormat("insert into haadbbcp values('{0}','{1}','{2}','{3}');",id ,treeView1SelectedNodeTag, dt1Rows[i][1]ToString(), dt1Rows[i][5]ToString());

dataGridView1Rows[0]Cells["编号"]Value = id;

ss += str;

}

if (stateEquals("modified", StringComparisonCurrentCultureIgnoreCase))

{

string str = stringFormat("update hbbcp set hbbcp_WLBH='{0}',hbbcp_bz='{1}' where HBBCP_ID='{2}';", dt1Rows[i][1]ToString(), dt1Rows[i][5]ToString(), dt1Rows[i]["编号"]ToString());

ss += str;

}

很明显,ResponseWrite(thisGridView1Rows[eRowIndex]Cells[1]Controls[0]ToString()Trim());

里的GridView1Rows[eRowIndex]Cells[1]Controls[0]ToString()是获得控件类型啊,想要得到控件的值就应该是 GridView1Rows[eRowIndex]Cells[i]Text

如果是主键,你可以到GridView里面去设定成如:

DataKeyNames="id"

<asp:GridView ID="gdvUserInfo" runat="server" AutoGenerateColumns="False" DataKeyNames="id">

获取的时候:

/// <summary>

/// 删除信息事件

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void gdvUserInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

string id =gdvUserInfoDataKeys[eRowIndex]ValueToString();

if (infoDeleteUserInfoById(id) > 0)

{

thislblMessageText = "删除成功!";

LoadInfo();

}

else

{

thislblMessageText = "删除失败!";

LoadInfo();

}

}

/// <summary>

/// 编辑事件

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void gdvUserInfo_RowEditing(object sender, GridViewEditEventArgs e)

{

gdvUserInfoEditIndex = eNewEditIndex;

LoadInfo();

}

/// <summary>

/// 取消编辑事件

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void gdvUserInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

{

gdvUserInfoEditIndex = -1;

LoadInfo();

}

/// <summary>

/// 更新事件

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void gdvUserInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

string userId = gdvUserInfoDataKeys[eRowIndex]ValueToString();

TextBox txtUserId = (TextBox)gdvUserInfoRows[eRowIndex]Cells[0]FindControl("txtUserId");

TextBox txtUserName = (TextBox)gdvUserInfoRows[eRowIndex]Cells[0]FindControl("txtUserName");

TextBox txtPassWord = (TextBox)gdvUserInfoRows[eRowIndex]Cells[0]FindControl("txtPassWord");

if (txtUserName != null && txtPassWord != null)

{

try

{

UserInfo user = infoGetUserInfoById(userId);

userUserName = txtUserNameTextToString();

userPassword = txtPassWordTextToString();

if (infoModifyUserInfo(user) > 0)

{

thislblMessageText = "修改成功!";

gdvUserInfoEditIndex = -1;

LoadInfo();

}

else

{

thislblMessageText = "修改失败!";

gdvUserInfoEditIndex = -1;

LoadInfo();

}

}

catch (Exception)

{

thislblMessageText = "修改失败!";

gdvUserInfoEditIndex = -1;

LoadInfo();

}

}

}

/// <summary>

///

/// 光棒事件

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void gdvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)

{

for (int i = 0; i < gdvUserInfoRowsCount; i++)

{

if (gdvUserInfoRows[i]RowType == DataControlRowTypeDataRow)

{

gdvUserInfoRows[i]AttributesAdd("onmouseover", "c=thisstylebackgroundColor;thisstylebackgroundColor='#9cf'");

gdvUserInfoRows[i]AttributesAdd("onmouseout", "thisstylebackgroundColor=c");

}

}

}

以上就是关于asp GridView1里面怎么取值啊 就是更新之后 要不值写入数据库全部的内容,包括:asp GridView1里面怎么取值啊 就是更新之后 要不值写入数据库、.net的gridview如何控制的更新和取消按钮的位置、C#datagridview问题在dgv中编辑数据点修改按钮后更新数据到数据库中。求大神赐教。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9546434.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存