public static void SetupGrid(string tablename, GridView grid, string searchStr, string treeStr, int pagesize, int nowpage, out int RecCount) { SortedList <int, Column> columns = new SortedList <int, Column>(); Table tab = new Table(tablename); foreach (Column c in tab.Columns) { if (c.IsListShow) { int key = c.ListShowOrder; while (columns.ContainsKey(key)) { key++; } columns.Add(key, c); } } string sql = "select " + tab.PKFiled; foreach (Column c in columns.Values) { if (sql == "select") { sql += " " + c.ColumnName; if (c.ListShowType == ListShowType.Foreign) { string fsql = "(select " + c.DataTextField + " from testnewstype where " + c.DataValueField + "=" + tablename + "." + c.ColumnName + ") as " + c.ColumnName + "_Foreign"; sql += "," + fsql; } } else { sql += "," + c.ColumnName; if (c.ListShowType == ListShowType.Foreign) { string fsql = "(select " + c.DataTextField + " from " + c.ForeignTable + " where " + c.DataValueField + "=" + tablename + "." + c.ColumnName + ") as " + c.ColumnName + "_Foreign"; sql += "," + fsql; } } DataControlField field = ListFieldFactory.GetListField(c.ListShowType.ToString(), tablename, c.ColumnName); grid.Columns.Add(field); } sql += " from " + tab.TableName + " where 1=1"; if ((!string.IsNullOrEmpty(searchStr)) && (!string.IsNullOrEmpty(tab.SearchColumn))) { sql += " and ("; string searchSql = string.Empty; string[] arr = searchStr.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); foreach (string str in arr) { if (!string.IsNullOrEmpty(searchSql)) { searchSql += " or "; } searchSql += tab.SearchColumn + " like '%" + searchStr.Replace("'", "''") + "%'"; } sql += searchSql; sql += ")"; } if ((!string.IsNullOrEmpty(treeStr)) && (!string.IsNullOrEmpty(tab.TreeColumn))) { sql += " and " + tab.TreeColumn + "='" + treeStr.Replace("'", "''") + "'"; } //Response.Write(sql); //Response.End(); if (!string.IsNullOrEmpty(tab.SelectOrder)) { if (tab.SelectOrder.Trim().ToLower().StartsWith("order by")) { sql += " " + tab.SelectOrder; } else { sql += " order by " + tab.SelectOrder; } } grid.AutoGenerateColumns = false; //添加排序 if (!string.IsNullOrEmpty(tab.OrderField)) { HyperLinkField orderup = new HyperLinkField(); orderup.Text = "<img src='images/up.jpg' border=0 />"; orderup.DataNavigateUrlFields = tab.PKFiled.Split(','); orderup.DataNavigateUrlFormatString = "OrderGO.aspx?id={0}&d=up&t=" + tablename; orderup.ItemStyle.Width = new Unit(20); grid.Columns.Add(orderup); HyperLinkField orderdown = new HyperLinkField(); orderdown.Text = "<img src='images/down.jpg' border=0 />"; orderdown.DataNavigateUrlFields = tab.PKFiled.Split(','); orderdown.DataNavigateUrlFormatString = "OrderGO.aspx?id={0}&d=down&t=" + tablename; orderdown.ItemStyle.Width = new Unit(20); grid.Columns.Add(orderdown); } //添加查看 if (tab.AllowShow) { HyperLinkField showField = new HyperLinkField(); showField.Text = "查看"; showField.DataNavigateUrlFields = tab.PKFiled.Split(','); showField.HeaderText = "查看"; showField.DataNavigateUrlFormatString = "show.aspx?id={0}&t=" + tablename + "&p=" + HttpContext.Current.Request.QueryString["p"] + "&m=" + HttpContext.Current.Request.QueryString["m"]; showField.ItemStyle.Width = new Unit(40); grid.Columns.Add(showField); } //添加编辑 if (tab.AllowEdit) { HyperLinkField editField = new HyperLinkField(); editField.Text = "编辑"; editField.DataNavigateUrlFields = tab.PKFiled.Split(','); editField.HeaderText = "编辑"; editField.DataNavigateUrlFormatString = "edit.aspx?id={0}&t=" + tablename + "&p=" + HttpContext.Current.Request.QueryString["p"] + "&m=" + HttpContext.Current.Request.QueryString["m"]; editField.ItemStyle.Width = new Unit(40); grid.Columns.Add(editField); } if (tab.AllowDel) { //添加删除 ConfirmLinkField delField = new ConfirmLinkField(); delField.Text = "删除"; delField.DataNavigateUrlFields = tab.PKFiled.Split(','); delField.DataNavigateUrlFormatString = "del.aspx?id={0}&t=" + tablename; delField.HeaderText = "删除"; delField.ItemStyle.Width = new Unit(40); grid.Columns.Add(delField); } //绑定 DataTable tabsource = DBFactory.GetConn().exeTable(sql); RecCount = tabsource.Rows.Count; if (pagesize > 0) { grid.DataSource = PagerHelper.GetPagedData(pagesize, nowpage, tabsource.DefaultView); grid.DataBind(); } else { grid.DataSource = tabsource; grid.DataBind(); } }