179 lines
5.3 KiB
C#
179 lines
5.3 KiB
C#
![]() |
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Web;
|
|||
|
using System.Web.UI;
|
|||
|
using System.Web.UI.WebControls;
|
|||
|
using System.Data;
|
|||
|
using System.Text;
|
|||
|
using System.IO;
|
|||
|
|
|||
|
namespace FineUI.Examples.grid
|
|||
|
{
|
|||
|
public partial class grid_editor_cell_delete : PageBase
|
|||
|
{
|
|||
|
protected void Page_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (!IsPostBack)
|
|||
|
{
|
|||
|
// 删除选中行
|
|||
|
btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("请至少选择一项!");
|
|||
|
btnDelete.ConfirmText = "确定删除选中单元格所在的行?";
|
|||
|
|
|||
|
|
|||
|
//btnDelete.ConfirmText = String.Format("你确定要删除第 <b><script>({0}[0]+1)</script></b> 行数据吗?", Grid1.GetSelectedCellReference());
|
|||
|
|
|||
|
//// 重置表格
|
|||
|
//btnReset.OnClientClick = Grid1.GetRejectChangesReference();
|
|||
|
|
|||
|
BindGrid();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#region BindGrid
|
|||
|
|
|||
|
private void BindGrid()
|
|||
|
{
|
|||
|
DataTable table = GetSourceData();
|
|||
|
|
|||
|
Grid1.DataSource = table;
|
|||
|
Grid1.DataBind();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Events
|
|||
|
|
|||
|
protected void btnDelete_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
StringBuilder sb = new StringBuilder();
|
|||
|
if (Grid1.SelectedCell != null)
|
|||
|
{
|
|||
|
// 注意设置表格的 DataIdField 属性,则 SelectedCell[0] 就是选中单元格所在的行ID
|
|||
|
int rowID = Convert.ToInt32(Grid1.SelectedCell[0]);
|
|||
|
DeleteRowByID(rowID);
|
|||
|
|
|||
|
BindGrid();
|
|||
|
|
|||
|
Alert.ShowInTop("删除数据成功!(表格数据已重新绑定)");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Alert.ShowInTop("没有选中任何单元格!");
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
|
|||
|
{
|
|||
|
if (e.CommandName == "Delete")
|
|||
|
{
|
|||
|
int rowID = Convert.ToInt32(Grid1.DataKeys[e.RowIndex][0]);
|
|||
|
DeleteRowByID(rowID);
|
|||
|
|
|||
|
BindGrid();
|
|||
|
|
|||
|
Alert.ShowInTop("删除数据成功!(表格数据已重新绑定)");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
protected void Button2_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();
|
|||
|
|
|||
|
labResult.Text = String.Format("用户修改的数据:<pre>{0}</pre>", Grid1.GetModifiedData().ToString(Newtonsoft.Json.Formatting.Indented));
|
|||
|
|
|||
|
Alert.Show("数据保存成功!(表格数据已重新绑定)");
|
|||
|
}
|
|||
|
|
|||
|
private void UpdateDataRow(Dictionary<string, object> rowDict, DataRow rowData)
|
|||
|
{
|
|||
|
// 姓名
|
|||
|
UpdateDataRow("Name", rowDict, rowData);
|
|||
|
|
|||
|
// 性别
|
|||
|
UpdateDataRow("Gender", rowDict, rowData);
|
|||
|
|
|||
|
// 入学年份
|
|||
|
UpdateDataRow("EntranceYear", rowDict, rowData);
|
|||
|
|
|||
|
// 入学日期
|
|||
|
UpdateDataRow("EntranceDate", rowDict, rowData);
|
|||
|
|
|||
|
// 是否在校
|
|||
|
UpdateDataRow("AtSchool", rowDict, rowData);
|
|||
|
|
|||
|
// 所学专业
|
|||
|
UpdateDataRow("Major", rowDict, rowData);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void UpdateDataRow(string columnName, Dictionary<string, object> rowDict, DataRow rowData)
|
|||
|
{
|
|||
|
if (rowDict.ContainsKey(columnName))
|
|||
|
{
|
|||
|
rowData[columnName] = rowDict[columnName];
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Data
|
|||
|
|
|||
|
private static readonly string KEY_FOR_DATASOURCE_SESSION = "datatable_for_grid_editor_cell_delete";
|
|||
|
|
|||
|
// 模拟在服务器端保存数据
|
|||
|
// 特别注意:在真实的开发环境中,不要在Session放置大量数据,否则会严重影响服务器性能
|
|||
|
private DataTable GetSourceData()
|
|||
|
{
|
|||
|
if (Session[KEY_FOR_DATASOURCE_SESSION] == null)
|
|||
|
{
|
|||
|
Session[KEY_FOR_DATASOURCE_SESSION] = DataSourceUtil.GetDataTable();
|
|||
|
}
|
|||
|
return (DataTable)Session[KEY_FOR_DATASOURCE_SESSION];
|
|||
|
}
|
|||
|
|
|||
|
// 根据行ID来获取行数据
|
|||
|
private DataRow FindRowByID(int rowID)
|
|||
|
{
|
|||
|
DataTable table = GetSourceData();
|
|||
|
foreach (DataRow row in table.Rows)
|
|||
|
{
|
|||
|
if (Convert.ToInt32(row["Id"]) == rowID)
|
|||
|
{
|
|||
|
return row;
|
|||
|
}
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
// 根据行ID来删除行数据
|
|||
|
private void DeleteRowByID(int rowID)
|
|||
|
{
|
|||
|
DataTable table = GetSourceData();
|
|||
|
|
|||
|
DataRow found = FindRowByID(rowID);
|
|||
|
if (found != null)
|
|||
|
{
|
|||
|
table.Rows.Remove(found);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|