后台创建columns并且为aspxgridview绑定数据代码如下:
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->string SelSql = "select * from bas_zsjfcsj where jh='" + TreeView1.SelectedNode.Text.ToString().Trim() + "' order by yczsh"; DataSet DS = DataHandle.ExecuteSQL(SelSql); GridViewCommandColumn xx = new GridViewCommandColumn(); xx.Caption = "编辑"; xx.NewButton.Visible = true; xx.DeleteButton.Visible = true; xx.EditButton.Visible = true; xx.NewButton.Text = "新建"; xx.EditButton.Text = "编辑"; xx.DeleteButton.Text = "删除"; xx.UpdateButton.Text = "更新"; xx.CancelButton.Text = "取消"; xx.VisibleIndex = 0; GridViewDataColumn x1 = new GridViewDataColumn(); x1.FieldName = "cenghao"; x1.VisibleIndex = 1; x1.Caption = "层号"; GridViewDataColumn x2 = new GridViewDataColumn(); x2.FieldName = "yczsh"; x2.VisibleIndex = 2; x2.Caption = "油藏中深(m)"; GridViewDataColumn x3 = new GridViewDataColumn(); x3.FieldName = "pzl"; x3.VisibleIndex = 2; x3.Caption = "配注量(m3)"; ASPxGridView1.Columns.Clear(); ASPxGridView1.Columns.Add(xx); ASPxGridView1.Columns.Add(x1); ASPxGridView1.Columns.Add(x2); ASPxGridView1.Columns.Add(x3); ASPxGridView1.KeyFieldName = "cenghao"; ASPxGridView1.DataSource = DS.Tables[0].DefaultView; Session["dsjc"] = DS; ASPxGridView1.DataBind();
点击表头自主排序代码:
protected void ASPxGridView1_BeforeColumnSortingGrouping(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewBeforeColumnGroupingSortingEventArgs e) { DataSet DS = (DataSet)Session["dsjc"]; if (DS == null) { return; } DataView DV = DS.Tables[0].DefaultView; switch (e.Column.FieldName) { case "cenghao": if ((SortOrder)e.OldSortOrder != SortOrder.Descending) { DV.Sort = "cenghao DESC"; } else { DV.Sort = "cenghao ASC" ; } break; case "yczsh": if ((SortOrder)e.OldSortOrder != SortOrder.Descending) { DV.Sort = "yczsh DESC"; } else { DV.Sort = "yczsh ASC"; } break; case "pzl": if ((SortOrder)e.OldSortOrder != SortOrder.Descending) { DV.Sort = "pzl DESC"; } else { DV.Sort = "pzl ASC"; } break; } ASPxGridView1.DataSource = DV; ASPxGridView1.DataBind(); }
错误提示栏代码:
protected void ASPxGridView1_CustomErrorText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomErrorTextEventArgs e) { if (Session["yczs"] == null) { e.ErrorText = "更新失败,更新项不是数值或者已经存在"; } }
数据删除事件代码:
protected void ASPxGridView1_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) { DataSet DS = (DataSet)Session["dsjc"]; DataRow[] DR = DS.Tables[0].Select("cenghao='" + e.Keys[0].ToString().Trim() + "'"); if (DR.Length > 0) { DR[0].Delete(); } if (Session["ModifyType"].ToString().Trim() != "Modify") { DS.AcceptChanges(); } ASPxGridView1.CancelEdit(); e.Cancel=true; ASPxGridView1.DataSource = DS.Tables[0].DefaultView; ASPxGridView1.DataBind(); }
数据插入代码:
protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) { Session["yczs"] = null; DataSet DS = (DataSet)Session["dsjc"]; if (e.NewValues[2] == null || e.NewValues[1] == null || e.NewValues[0] == null) { return; } if (!IsNumberic(e.NewValues[2].ToString().Trim()) || !IsNumberic(e.NewValues[1].ToString().Trim())) { return; } DataRow[] DR = DS.Tables[0].Select("cenghao='" + e.NewValues[0].ToString().Trim() + "'"); if(DR.Length>0) { return; } if (rgjd.Text.Trim() != "") { if (double.Parse(e.NewValues[1].ToString().Trim()) > double.Parse(rgjd.Text.Trim())) { Session["yczs"] = "large"; return; } } DataRow DRNew = DS.Tables[0].NewRow(); DRNew["cenghao"] = e.NewValues[0].ToString().Trim(); DRNew["yczsh"] = double.Parse(e.NewValues[1].ToString().Trim()); DRNew["pzl"] = double.Parse(e.NewValues[2].ToString().Trim()); if (Session["ModifyType"].ToString().Trim() == "Modify") { DRNew["jh"] = TreeView1.SelectedNode.Text.Trim(); } DS.Tables[0].Rows.Add(DRNew); ASPxGridView1.CancelEdit(); e.Cancel=true; ASPxGridView1.DataSource = DS.Tables[0].DefaultView; ASPxGridView1.DataBind(); }
数据更新代码:
protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) { Session["yczs"] = null; DataSet DS = (DataSet)Session["dsjc"]; if (!IsNumberic(e.NewValues[2].ToString().Trim()) || !IsNumberic(e.NewValues[1].ToString().Trim())) { return; } if (e.Keys[0].ToString().Trim() != e.NewValues[0].ToString().Trim()) { DataRow[] dr = DS.Tables[0].Select("cenghao='" + e.NewValues[0].ToString().Trim() + "'"); if (dr.Length > 0) { return; } } if (rgjd.Text.Trim() == "") { if (double.Parse(e.NewValues[1].ToString().Trim()) > double.Parse(rgjd.Text.Trim())) { Session["yczs"] = "large"; return; } } DataRow[] DRSelf = DS.Tables[0].Select("cenghao='" + e.Keys[0].ToString().Trim() + "'"); if (DRSelf.Length > 0) { DRSelf[0]["yczsh"] = double.Parse(e.NewValues[1].ToString().Trim()); DRSelf[0]["pzl"] = double.Parse(e.NewValues[2].ToString().Trim()); DRSelf[0]["cenghao"] = e.NewValues[0].ToString().Trim(); } ASPxGridView1.CancelEdit(); e.Cancel = true; ASPxGridView1.DataSource = DS.Tables[0].DefaultView; ASPxGridView1.DataBind(); }
这些代码基本上没有什么技术含量,仅仅是提供一种方法让习惯用ADO.NET后台绑定的兄弟们使用。Dev控件系列在样式和操作风格下非常强大。例如这个ASPXGridView,风格非常大方,数据操作也符合用户习惯。这里仅仅介绍了一小部分,还有其他的模板列操作,弹出层数据操作,动态数据操作等等很多强大功能。顺便介绍一下使用过程种的问题:
- Dev控件发布的时候,必须先找到web.config中所引用的dll名字到dev控件安装程序中的dll库种复制相应的dll到发布完的bin目录下,同时必须确保dev控件为已注册版本。
- ASPXGridview控件使用过程中,不管是使用自带的样式还是自主定义的样式,尽量确保页面第一次pageload的时候ASPXGridView控件是可见,如果第一次pageload的时候ASPXGridview控件不可见,我们会发现通过事件将gridview更改为可见的时候,gridview的定义的样式已经没有了,并且编辑按钮也已经失效(可能是本人还没完全了解这个控件吧,期待有人告诉原因与解决方法)。这个问题在我们用提示框是否删除或者提示删除成功的时候也会出现,目前我的解决方法是将ASPX页和引用的样式theme在同一路径下。
- aspxgridview控件也支持模板系列,可以研究下官方的demo