同步取得GricdView中更新的数据
编程中好多时候都会遇到要根据GricdView中的数据更新到数据库中,就会带来一个问题:如何取得GricdView中的新数据?下面提供一种解决此问题的方法:
private DataTable SyncGricdViewSelectedItemToSession()
{
dt = (DataTable)Session[SearchResultDt]; //取得要更新的Session DataTable
//定义一个key
DataColumn[] keys = new DataColumn[2];
DataColumn fpn_numcolumn = dt.Columns["NUM"];
DataColumn transactionidcolumn = dt.Columns["TransactionID"];
keys[0] = fpn_numcolumn;
keys[1] = transactionidcolumn;
dt.PrimaryKey = keys;
//-------------
CheckBox chkSelect = null;
TextBox txtDishAmount = null;
Label lblDishonoredAmount = null;
string sNum = null;
string sTransactionid = null;
string[] tempkeys = null;
bool isEnableAmonut = false;
bool isShow = false;
DataRow dr = null;
CheckBox chkSelectAll = (CheckBox)gvResult.HeaderRow.FindControl("chkSelectAll");
if (chkSelectAll.Checked)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
isEnableAmonut = Convert.ToBoolean(dt.Rows[i]["EnableAmonut"].ToString().Trim());
isShow = Convert.ToBoolean(dt.Rows[i]["HasShow"].ToString().Trim());
if (isShow && isEnableAmonut)
{
dt.Rows[i]["chkSelect"] = "true";
if (i < gvResult.Rows.Count)
{
chkSelect = (CheckBox)gvResult.Rows[i].FindControl("chkSelect");
txtAmount = (TextBox)gvResult.Rows[i].FindControl("txtAmount");
lblAmount = (Label)gvResult.Rows[i].FindControl("lblAmount");
sNum = ((HiddenField)gvResult.Rows[i].FindControl("hdno")).Value;
sTransactionid = ((HiddenField)gvResult.Rows[i].FindControl("hdftransactionid")).Value;
tempkeys = new string[] { sNum, sTransactionid };
dr = dt.Rows.Find(tempkeys);
if (dr != null)
{
if (isEnableAmonut)
{
if (dr["Amount"].ToString() == "")
{
dr["Amount"] = txtAmount.Text.Trim().Replace(",", "");
dt.Rows[i]["chkSelect"] = "True";
}
}
else
{
if (dr["Amount"].ToString() == "")
{
dr["Amount"] = lblAmount.Text.Trim().Replace(",", "");
}
}
}
}
}
else
{
dt.Rows[i]["chkSelect"] = "false";
}
}
}
else
{
//check those selected items.
for (int j = 0; j < gvResult.Rows.Count; j++)
{
isEnableAmonut = Convert.ToBoolean(((HiddenField)gvResult.Rows[j].FindControl("hdfEnableAmonut")).Value);
chkSelect = (CheckBox)gvResult.Rows[j].FindControl("chkSelect");
txtAmount = (TextBox)gvResult.Rows[j].FindControl("txtAmount");
lblAmount = (Label)gvResult.Rows[j].FindControl("lblAmount");
sNum = ((HiddenField)gvResult.Rows[j].FindControl("hdno")).Value;
sTransactionid = ((HiddenField)gvResult.Rows[j].FindControl("hdftransactionid")).Value;
tempkeys = new string[] { sNum, sTransactionid };
dr = dt.Rows.Find(tempkeys);
if (dr != null)
{
dr["chkSelect"] = chkSelect.Checked.ToString();
if (isEnableAmonut)
{
dr["Amount"] = txtAmount.Text.Trim().Replace(",", "");
}
else
{
dr["Amount"] = lblAmount.Text.Trim().Replace(",", "");
}
}
}
}
return dt;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构