void WshPwtGv_RowDataBound(object sender, GridViewRowEventArgs e) { List <ColInfo> colLists = this.ColList; if (colLists == null) { colLists = GetColInfo(); } if (e.Row.RowType == DataControlRowType.Header) { TableCellCollection cells = e.Row.Cells; for (int j = 1; j < cells.Count; j++) { //cells[j].Attributes.Add("ColName", colLists[j - 1].FieldName); if (colLists[j - 1].Sort == false) { Literal lSort = new Literal(); lSort.Text = colLists[j - 1].Caption; cells[j].Controls.Add(lSort); } if (WshPwtGv.Columns[j].SortExpression == this.SortName) { Image sortimg = new Image(); sortimg.Style.Add("margin", "0px 2px"); if (this.SortMode == "desc") { sortimg.ImageUrl = "~/images/GridImg/downarrow.gif"; } else { sortimg.ImageUrl = "~/images/GridImg/uparrow.gif"; } cells[j].Controls.Add(sortimg); } //if (colLists[j - 1].EditType != "" && OnCellEdit!=null) //{ // //cells[j].Attributes.Add("ColEdit", "ColEdit"); // Literal lit = new Literal(); // lit.Text = "<span style='color:red;font-size:11px;font-weight:normal;margin-left:2px'>edit</span>"; // cells[j].Controls.Add(lit); //} } } string datakey = null; if (e.Row.RowType == DataControlRowType.DataRow) { //添加行的js属性,用来保存主键 datakey = this.WshPwtGv.DataKeys[e.Row.RowIndex].Value.ToString(); e.Row.Attributes.Add("dataKey", datakey); e.Row.Attributes.Add("rowType", "dataRow"); //为单元格绑定列名 List <CellValue> cValue = this.SetCellValue; TableCellCollection cells = e.Row.Cells; for (int j = 1; j < cells.Count; j++) { //if(colLists[j-1].Width>0){ // cells[j].Style.Add("width", colLists[j - 1].Width+"px"); //} string oldText = cells[j].Text; string colName = colLists[j - 1].Field; cells[j].Attributes.Add("colName", colName); Enums.ControlType edittype = colLists[j - 1].EditType; Literal lit = new Literal(); CellValue cv = new CellValue(); //可以编辑的单元格 if (edittype != Enums.ControlType.None) { cells[j].Attributes.Add("onclick", "$(this).cellEdit();"); cells[j].Attributes.Add("editType", colLists[j - 1].EditType.ToString()); //cells[j].Attributes.Add("class","editor"); } if (edittype == Enums.ControlType.Text) { lit.Text = "<div class='editor' val='" + oldText + "'>" + oldText + "</div><input type='text' value='" + oldText + "' class='editInput' onblur='$(this).blurEdit();' onchange='$(this).saveEdit();'/>"; if (cValue != null) { foreach (CellValue newCellVal in cValue) { if (colLists[j - 1].Field == newCellVal.ColName) { cv = newCellVal; } } } cells[j].Controls.Add(lit); } if (edittype == Enums.ControlType.Combobox) { StringBuilder sb = new StringBuilder(); string val = oldText; sb.AppendFormat("<select style='display:none;width:100%;' onblur='$(this).blurEdit();' onchange='$(this).saveEdit();'>"); if (this.EditValue != null) { object values = EditValue[colName]; if (values != null) { string[] p = values.ToString().Split(",".ToCharArray()); for (int k = 0; k < p.Length; k++) { string[] c = p[k].Split("=".ToCharArray()); string selected = ""; if (oldText == c[0]) { selected = " selected='selected'"; oldText = c[1]; } sb.AppendFormat("<option value='{0}'" + selected + ">{1}</option>", c[0], c[1]); } } } sb.Append("</select>"); sb.Append("<div class='editor' val='" + val + "'>" + oldText + "</div>"); lit.Text = sb.ToString(); cells[j].Controls.Add(lit); } } } //执行自定义绑定事件 if (OnRowBind != null) { RowBindEventArg Args = new RowBindEventArg(); Args.Row = e.Row; Args.DataKey = datakey; OnRowBind(this, Args); } }
public override void DataBind() { DataTable dt = this.DataSource; List <ColInfo> cols = this.Columns; if (dt != null && dt.Rows.Count > 0) { HtmlGenericControl tbodyPanal = new HtmlGenericControl("div"); tbodyPanal.Attributes.Add("class", "datagrid-tbody-panal"); // tbodyPanal.Style.Add("height", this.Height); tbody.CellPadding = 0; tbody.CellSpacing = 0; tbody.ID = "tbody"; tbody.Attributes.Add("class", "datagrid-tbody-table"); // tbody.Attributes.Add("rules", "all"); //绑定数据源 for (int i = 0; i < dt.Rows.Count; i++) { string dataKey = dt.Rows[i][this.DataKey].ToString(); HtmlTableRow row = new HtmlTableRow(); row.Attributes.Add("dataKey", dataKey); row.Attributes.Add("rowType", "dataRow"); row.Attributes.Add("class", i % 2 == 0 ? "roweven" : "rowodd"); if (this.ShowCheckColumn == true) { HtmlTableCell checkcell = new HtmlTableCell(); checkcell.Style.Add("width", this.checkWidth + "px"); HtmlInputCheckBox box = new HtmlInputCheckBox(); box.Attributes.Add("multiCheck", "true"); //box.Attributes.Add("onclick", ""); checkcell.Controls.Add(box); row.Controls.Add(checkcell); } for (int j = 0; j < cols.Count; j++) { string txt = dt.Rows[i][cols[j].Field].ToString(); HtmlTableCell cell = new HtmlTableCell(); //设置宽度 int w = cols[j].Width; cell.Style.Add("width", w + "px"); if (cols[j].Full == true) { cell.Attributes.Add("fullColumn", "true"); } if (cols[j].Align != "") { cell.Style.Add("text-align", cols[j].Align); } Enums.ControlType editType = cols[j].EditType; HtmlGenericControl cellChild = new HtmlGenericControl("div"); cellChild.Attributes.Add("class", "datagrid-tbody-cellChild datagrid-cell-nowrap" + (editType == Enums.ControlType.None ? "" : " editor")); //装进文本 HtmlGenericControl celltxt = new HtmlGenericControl("span"); celltxt.InnerHtml = txt; celltxt.ID = Utils.GetGuid; cellChild.Controls.Add(celltxt); //是否可以编辑 string colName = cols[j].Field; if (editType != Enums.ControlType.None) { cellChild.Attributes.Add("editType", editType.ToString()); cellChild.Attributes.Add("colName", colName); cellChild.Attributes.Add("onclick", "$(this).datagridEditStart();"); //文本框 if (editType == Enums.ControlType.Text) { HtmlInputText textbox = new HtmlInputText(); textbox.Attributes.Add("class", "text datagrid-editinput"); cellChild.Controls.Add(textbox); textbox.Attributes.Add("onblur", "$(this).datagridEditEnd();"); textbox.Attributes.Add("onchange", "$(this).datagridEditSave();"); } } cell.Controls.Add(cellChild); //执行单元格创建完毕事件 if (this.OnCellCreated != null) { CellEditEventArg arg = new CellEditEventArg(); arg.ColName = cols[j].Field; arg.DataKey = dataKey; arg.Value = txt; this.OnCellCreated(this, arg); } row.Controls.Add(cell); } tbody.Controls.Add(row); } tbody.Style.Add("width", tableWidth + "px"); tbodyPanal.Controls.Add(tbody); this.GridPanal.Controls.Add(tbodyPanal); } else { //显示空表体 } }