各位大神,我根据实例http://fineui.com/demo/#/demo/grid/grid_editor_cell.aspx也做了一个页面,但是点保存之后数据库的数据没变化,但是lable3里面显示的内容是对的。后台代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using FineUI;
namespace EmptyProjectNet40_FineUI
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Dictionary<int, Dictionary<string, object>> modifiedDict = Grid1.GetModifiedDict();
foreach (int rowIndex in modifiedDict.Keys)
{
int rowID = Convert.ToInt32(Grid1.DataKeys[rowIndex][0]);
DataRow row = FindRowByID(rowID);
UpdateDataRow(modifiedDict[rowIndex], row);
}
BindGrid();
Label3.Text = "用户修改的数据:" + Grid1.GetModifiedData().ToString(Newtonsoft.Json.Formatting.None);
Alert.Show("数据保存成功!(表格数据已重新绑定)");
}
private void UpdateDataRow(Dictionary<string, object> rowDict, DataRow rowData)
{
// 姓名
UpdateDataRow("item", rowDict, rowData);
// 性别
UpdateDataRow("po", rowDict, rowData);
// 入学年份
UpdateDataRow("name", rowDict, rowData);
// 入学日期
UpdateDataRow("color1", rowDict, rowData);
// 是否在校
UpdateDataRow("color2", rowDict, rowData);
// 所学专业
UpdateDataRow("date", rowDict, rowData);
}
private void UpdateDataRow(string columnName, Dictionary<string, object> rowDict, DataRow rowData)
{
if (rowDict.ContainsKey(columnName))
{
rowData[columnName] = rowDict[columnName];
}
}
private void BindGrid()
{
DataTable dt = GetSourceData();
Grid1.DataSource = dt;
Grid1.DataBind();
}
private DataTable GetSourceData()
{
string strconn;
string sql = "select item,po,name,color1,color2,client,number,date,price,amount,id from orderlist";
strconn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(sql,conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
conn.Close();
return dt;
}
private DataRow FindRowByID(int rowID)
{
DataTable table = GetSourceData();
foreach (DataRow row in table.Rows)
{
if (Convert.ToInt32(row["id"]) == rowID)
{
return row;
}
}
return null;
}
}
}
是少了什么吗?
|