怎样将LIST中的数据绑定到GridView,并实现增删改查
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样将LIST中的数据绑定到GridView,并实现增删改查相关的知识,希望对你有一定的参考价值。
我的一个GridView操作文件,数据源是实体LIST,删改查功能都有了,你参考下using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.htmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using lwf_MVC.CTRL;
using lwf_MVC.lwf_Normal;
using lwf_MVC.MODEL;
public partial class admin_news_edit : System.Web.UI.Page
public string icls_name = "cls_f_news";
//不同级数系列区别在于iWhere
public string iWhere = " 1 = 1 and ENG = " + WEB.GQ("ENG", "0") + " and TID1 = " + WEB.GQ("SSS", "1") + " and 1 = 1";
public int getRecordCount()
cls_f_news if_news = (cls_f_news)CTRL.CreateModel(icls_name);
if_news.title = soKey.Text;
if_news.contents = soKey.Text;
if_news.WHERE = iWhere;
int i_recordcount = CTRL.getCount(if_news, "order by orderid,id desc");
return i_recordcount;
protected void Page_Load(object sender, EventArgs e)
if (WEB.getSession("userName") == null || WEB.getSession("userName") == "") JS.RD(System.Web.HttpUtility.UrlDecode("%e7%99%bb%e9%99%86%e8%b6%85%e6%97%b6%2c%e8%af%b7%e9%87%8d%e6%96%b0%e7%99%bb").Trim(), "../login.aspx");
if (!IsPostBack)
AspNetPagerBin(1);
GridViewBin();
protected void AspNetPagerBin(int curr)
int i_recordcount = getRecordCount();
AspNetPager1.RecordCount = i_recordcount;
AspNetPager1.CurrentPageIndex = curr;
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
GridView1.EditIndex = e.NewEditIndex;
GridViewBin();
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
GridView1.EditIndex = -1;
GridViewBin();
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
int i_id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
cls_f_news baseTableObj = (cls_f_news)CTRL.CreateModel(icls_name, i_id);
baseTableObj.title = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
baseTableObj.newfrom = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString();
baseTableObj.addTime = DateTime.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString());
baseTableObj.orderid = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].FindControl("tb_orderid_b")).Text.ToString());
if (CTRL.Update(baseTableObj))
JS.alert(System.Web.HttpUtility.UrlDecode("%e4%bf%ae%e6%94%b9%e6%88%90%e5%8a%9f").Trim());
else
JS.alert(System.Web.HttpUtility.UrlDecode("%e7%bd%91%e7%bb%9c%e8%b6%85%e6%97%b6%ef%bc%8c%e8%af%b7%e9%87%8d%e6%96%b0%e5%86%8d%e8%af%95").Trim());
//====================================
GridView1.EditIndex = -1;
GridViewBin();
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
int i_id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
cls_f_news baseTableObj = (cls_f_news)CTRL.CreateModel(icls_name, i_id);
if (CTRL.Delete(baseTableObj))
JS.alert(System.Web.HttpUtility.UrlDecode("%e8%ae%b0%e5%bd%95%e5%88%a0%e9%99%a4%e6%88%90%e5%8a%9f").Trim());
else
JS.alert(System.Web.HttpUtility.UrlDecode("%e7%bd%91%e7%bb%9c%e8%b6%85%e6%97%b6%ef%bc%8c%e8%af%b7%e9%87%8d%e6%96%b0%e5%86%8d%e8%af%95").Trim());
//====================================
GridView1.EditIndex = -1;
GridViewBin();
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
GridView1.EditIndex = -1;
GridView1.PageIndex = AspNetPager1.CurrentPageIndex;
GridViewBin();
protected void CheckBox_all_CheckedChanged(object sender, EventArgs e)
CheckBox_opp.Checked = false;
for (int i_i = 0; i_i <= GridView1.Rows.Count - 1; i_i++)
CheckBox ckb = (CheckBox)GridView1.Rows[i_i].FindControl("cb_selBoxs");
if (CheckBox_all.Checked)
ckb.Checked = true;
else
ckb.Checked = false;
protected void CheckBox_opp_CheckedChanged(object sender, EventArgs e)
CheckBox_all.Checked = false;
for (int i_i = 0; i_i <= GridView1.Rows.Count - 1; i_i++)
CheckBox ckb = (CheckBox)GridView1.Rows[i_i].FindControl("cb_selBoxs");
if (ckb.Checked)
ckb.Checked = false;
else
ckb.Checked = true;
protected void lb_delete_Click(object sender, EventArgs e)
for (int i_i = 0; i_i <= GridView1.Rows.Count - 1; i_i++)
CheckBox ckb = (CheckBox)GridView1.Rows[i_i].FindControl("cb_selBoxs");
if (ckb.Checked)
int i_id = int.Parse(GridView1.DataKeys[i_i].Value.ToString());
cls_f_news baseTableObj = (cls_f_news)CTRL.CreateModel(icls_name, i_id);
CTRL.Delete(baseTableObj);
int i_Pcount = getRecordCount();
if (AspNetPager1.CurrentPageIndex > i_Pcount)
AspNetPagerBin(i_Pcount);
else
AspNetPagerBin(AspNetPager1.CurrentPageIndex);
GridViewBin();
protected void lb_order_Click(object sender, EventArgs e)
for (int i_i = 0; i_i <= GridView1.Rows.Count - 1; i_i++)
TextBox tb = (TextBox)GridView1.Rows[i_i].FindControl("tb_orderid_a");
int i_id = int.Parse(GridView1.DataKeys[i_i].Value.ToString());
cls_f_news baseTableObj = (cls_f_news)CTRL.CreateModel(icls_name, i_id);
baseTableObj.orderid = int.Parse(tb.Text.ToString());
CTRL.Update(baseTableObj);
GridViewBin();
protected void bt_search_Click(object sender, EventArgs e)
AspNetPagerBin(1);
GridViewBin();
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
int rowNum;
int id_i;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
string sortExpression = e.SortExpression;
ViewState["sortExpression"] = sortExpression;
if (ViewState["ASC"].ToString() == "0")
ViewState["ASC"] = "1";
else
ViewState["ASC"] = "0";
AspNetPagerBin(1);
GridViewBin();
protected void GridViewBin()
CheckBox_all.Checked = false;
cls_f_news if_news = (cls_f_news)CTRL.CreateModel(icls_name);
if_news.title = soKey.Text;
if_news.contents = soKey.Text;
if_news.WHERE = iWhere;
int RecordCount;
int curP = Convert.ToInt32(AspNetPager1.CurrentPageIndex);
List<cls_f_news> iList_f_news = CTRL.getPageList(if_news, "order by orderid,id desc", AspNetPager1.PageSize, curP, out RecordCount).ConvertAll<cls_f_news>(delegate(lwf_MVC.MODEL.baseTable s) return (cls_f_news)s; );
if (ViewState["ASC"] == null) ViewState["ASC"] = "-1";
if (ViewState["sortExpression"] == null) ViewState["sortExpression"] = "orderid";
if (ViewState["ASC"].ToString() == "-1")
iList_f_news.Sort(delegate(cls_f_news a, cls_f_news b) return (new CaseInsensitiveComparer()).Compare(a.GetType().GetProperty(ViewState["sortExpression"].ToString()).GetValue(a, null), b.GetType().GetProperty(ViewState["sortExpression"].ToString()).GetValue(b, null)); );
else
if (ViewState["ASC"].ToString() == "1")
iList_f_news.Sort(delegate(cls_f_news a, cls_f_news b) return (new CaseInsensitiveComparer()).Compare(a.GetType().GetProperty(ViewState["sortExpression"].ToString()).GetValue(a, null), b.GetType().GetProperty(ViewState["sortExpression"].ToString()).GetValue(b, null)); );
else
iList_f_news.Sort(delegate(cls_f_news a, cls_f_news b) return (new CaseInsensitiveComparer()).Compare(b.GetType().GetProperty(ViewState["sortExpression"].ToString()).GetValue(b, null), a.GetType().GetProperty(ViewState["sortExpression"].ToString()).GetValue(a, null)); );
GridView1.DataSource = iList_f_news;
GridView1.DataKeyNames = new string[] "id" ;
GridView1.DataBind();
参考技术A 给你个很笨的办法,在页面调用后台的方法,获取数据,显示 参考技术B 装箱,装到datatable中去
关于怎样获取DevExpress GridView过滤后或排序后的数据集问题(转)
GridView用自带的过滤功能过滤数据后,想要获取过滤后的数据集,有两种方式:
一、笨办法就是循环遍历GridView,根据gridView.GetRow()或者gridView.GetDataRow()(该方法返回DataRow类型,使用于数据源是DataTable)去获取指定handle对应的数据对象。
1、若GridControl绑定的数据源是List类型,可以调用以下方法
/// <summary>
/// 获取GridView过滤或排序后的数据集
/// </summary>
/// <typeparam name="T">泛型对象</typeparam>
/// <param name="view">GridView</param>
/// <returns></returns>
public IEnumerable<T> GetGridViewFilteredAndSortedData<T>(DevExpress.XtraGrid.Views.Grid.GridView view) where T : class
{
var list = new List<T>();
for (int i = 0; i < view.RowCount; i++)
{
if (view.IsGroupRow(i))
continue;
var entity = view.GetRow(i) as T;
if (entity == null)
continue;
list.Add(entity);
}
return list;
}
2、若GridControl绑定的数据源是DataTable类型,可以调用以下方法:
<pre name="code" class="csharp"> /// <summary>
/// 获取GridView过滤或排序后的数据集
/// </summary>
/// <param name="view">GridView</param>
/// <returns></returns>
public DataTable GetGridViewFilteredAndSortedDataToDataTable(DevExpress.XtraGrid.Views.Grid.GridView view)
{
DataTable _dt = view.GridControl.DataSource as DataTable;
if (_dt == null)
return null;
DataTable dt = _dt.Clone();
for (int i = 0; i < view.RowCount; i++)
{
if (view.IsGroupRow(i))
continue;
var dr = view.GetDataRow(i);
if (dr == null)
continue;
dt.Rows.Add(dr.ItemArray);
}
return dt;
}
第二种方法:估计大伙很难找到,DevExpress 的GridView竟然提供了相应的方法,但是奇怪的是编译器竟然没有显示,就是在GridView的基类BaseView中提供了DataController这个对象,查看了这个属性后发现竟然真的被屏蔽了,EditorBrowsable竟然是Never状态(我也表示一脸懵逼)
当然,屏蔽了,不代表他没有这个属性,那就一脸懵逼的使用吧,DataController提供一个获取过滤后和排序后的数据集的方法:GetAllFilteredAndSortedRows()
为了不每次都懵逼的去这样直接调用,最好还是写一个公用方法去调用吧
<pre name="code" class="csharp"> /// <summary>
/// 获取GridView过滤或排序后的数据集
/// </summary>
/// <param name="view"></param>
/// <returns></returns>
public System.Collections.IList GetGridViewFilteredAndSortedData(DevExpress.XtraGrid.Views.Grid.GridView view)
{
return view.DataController.GetAllFilteredAndSortedRows();
}
---------------------
作者:涛神-Dev
来源:CSDN
原文:https://blog.csdn.net/u012097590/article/details/52777437
版权声明:本文为博主原创文章,转载请附上博文链接!
以上是关于怎样将LIST中的数据绑定到GridView,并实现增删改查的主要内容,如果未能解决你的问题,请参考以下文章
如何将值存储在列表或数组中并将所有值绑定到数据表然后gridview
如何将json返回的数据绑定到asp.net中的jquery gridview?