public void GridViewCheck(DevExpress.XtraGrid.Views.Grid.GridView gridView, string p, int id) { if (p == "user") { List<Users> listRelated = new List<Users>(); listRelated = WcfServiceLocator.Create<IUsersManage>().FindRelatedUsersForUserGroup(id); //找出和指定用户组关联的用户 for (int i = 0; i < gridView.RowCount; i++) { var check = listRelated.Exists(delegate(Users user) { if (user.id == (int)gridView.GetRowCellValue(i, "id")) return true; return false; }); gridView.SetRowCellValue(i, "isChecked", check); } } if (p == "role") { List<Users> listRelated = new List<Users>(); listRelated = WcfServiceLocator.Create<IRoleManage>().findRelatedUser(id); //找出和指定角色关联的用户 for (int i = 0; i < gridView.RowCount; i++) { var check = listRelated.Exists(delegate(Users user) { if (user.id == (int)gridView.GetRowCellValue(i, "id")) return true; return false; }); gridView.SetRowCellValue(i, "isChecked", check); } } if (p == "rolewithgroup") { List<Group> listRelated = new List<Group>(); listRelated = WcfServiceLocator.Create<IRoleManage>().findRelatedGroup(id); //找出和指定角色关联的用户 for (int i = 0; i < gridView.RowCount; i++) { var check = listRelated.Exists(delegate(Group group) { if (group.Id == (int)gridView.GetRowCellValue(i, "Id")) return true; return false; }); gridView.SetRowCellValue(i, "isChecked", check); } } }
private int GetCount(DevExpress.XtraGrid.Views.Grid.GridView gv) { if (gv==null||gv.RowCount<=0) { return 0; } try { int Count = 0; for (int i = 0; i < gv.RowCount; i++) { Count = Count + int.Parse(gv.GetRowCellValue(i, "COUNT").ToString()); } return Count; } catch (System.Exception ex) { return 0; } }
public static List<structColumn> GetListColumnsVisible(DevExpress.XtraGrid.Views.BandedGrid.BandedGridView _BandedGridView) { List<structColumn> ListColumns = new List<structColumn>(); try { if (_BandedGridView != null) { for (int i = 0; i < _BandedGridView.Columns.Count; i++) { if (_BandedGridView.Columns[i].Visible && _BandedGridView.Columns[i].GroupIndex < 0) { structColumn _structColumn = new structColumn(); _structColumn.Caption = _BandedGridView.Columns[i].Caption; _structColumn.FieldName = _BandedGridView.Columns[i].Name + "_" + _BandedGridView.Columns[i].FieldName; _structColumn.VisibleIndex = _BandedGridView.Columns[i].VisibleIndex; _structColumn.IsNumber = false; if (!Object.Equals(_BandedGridView.GetRowCellValue(0, _BandedGridView.Columns[i]), null)) { if (SHARED.Libraries.StringHelper.IsNumber(_BandedGridView.GetRowCellValue(0, _BandedGridView.Columns[i]).ToString())) { _structColumn.IsNumber = true; } } ListColumns.Add(_structColumn); } } ListColumns = ListColumns.OrderBy(item => item.VisibleIndex).ToList(); } } catch { } return ListColumns; }
public static DataSet FillDatasetFromGrid(DevExpress.XtraGrid.Views.BandedGrid.BandedGridView _BandedGridView) { DataSet _DataSet = new DataSet(); DataTable _DataTable = new DataTable(); if (!Object.Equals(_BandedGridView, null)) { foreach (GridColumn _GridColumn in _BandedGridView.Columns) { _DataTable.Columns.Add(_GridColumn.Name + "_" + _GridColumn.FieldName, Nullable.GetUnderlyingType(_GridColumn.ColumnType) ?? _GridColumn.ColumnType); if (!(_GridColumn.Visible && _GridColumn.GroupIndex < 0)) { if (Object.Equals(_GridColumn.ColumnType, typeof(Int32)) || Object.Equals(_GridColumn.ColumnType, typeof(float)) || Object.Equals(_GridColumn.ColumnType, typeof(DateTime))) { _DataTable.Columns.Add(_GridColumn.Name + "_" + _GridColumn.FieldName + "_STRING", typeof(String)); } } } for (int i = 0; i < _BandedGridView.DataRowCount; i++) { DataRow _DataRow = _DataTable.NewRow(); foreach (GridColumn _GridColumn in _BandedGridView.Columns) { var Value = _BandedGridView.GetRowCellValue(i, _GridColumn); if (Object.Equals(Value, null)) { _DataRow[_GridColumn.Name + "_" + _GridColumn.FieldName] = DBNull.Value; } else { _DataRow[_GridColumn.Name + "_" + _GridColumn.FieldName] = _BandedGridView.GetRowCellValue(i, _GridColumn); } if (!(_GridColumn.Visible && _GridColumn.GroupIndex < 0)) { if (Object.Equals(_GridColumn.ColumnType, typeof(Int32)) || Object.Equals(_GridColumn.ColumnType, typeof(float)) || Object.Equals(_GridColumn.ColumnType, typeof(DateTime))) { var Value_String = _BandedGridView.GetRowCellValue(i, _GridColumn); if (Object.Equals(Value_String, null)) { _DataRow[_GridColumn.Name + "_" + _GridColumn.FieldName + "_STRING"] = ""; } else { _DataRow[_GridColumn.Name + "_" + _GridColumn.FieldName + "_STRING"] = _BandedGridView.GetRowCellValue(i, _GridColumn); } } } } _DataTable.Rows.Add(_DataRow); } } _DataSet.Tables.Add(_DataTable); return _DataSet; }
/// <summary> /// 可以自定义导出Excel的格式,传的参数为GridView /// </summary> /// <param name="gridView">需要保存的GridView</param> /// <param name="sheetName">Sheet名</param> /// <param name="filename">文件名</param> public static void ExportGridViewToExcel(DevExpress.XtraGrid.Views.Grid.GridView gridView, string sheetName, string filename) { //System.Data.DataTable dt = (System.Data.DataTable)gridView.DataSource; SaveFileDialog sfd = new SaveFileDialog(); sfd.FileName = filename; sfd.Filter = "Excel files (*xls) | *.xls"; sfd.RestoreDirectory = true; if (sfd.ShowDialog() == DialogResult.OK && sfd.FileName.Trim() != null) { int colIndex = 0; System.Reflection.Missing miss = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application(); xlapp.Visible = false; //是否显示导出过程 ,建议关闭,否则在导出过程中鼠标点击Excel文件时会出错。 Microsoft.Office.Interop.Excel.Workbooks mBooks = (Microsoft.Office.Interop.Excel.Workbooks)xlapp.Workbooks; Microsoft.Office.Interop.Excel.Workbook mBook = (Microsoft.Office.Interop.Excel.Workbook)mBooks.Add(miss); Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)mBook.Worksheets.Add(miss, miss, 0, Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet); //设置文字自动换行 //mSheet.Cells.WrapText = true; try { int gridViewIndex = 1; int rowIndex = 1; sheet.Name = sheetName; //设置对齐方式 sheet.Cells.HorizontalAlignment = XlHAlign.xlHAlignCenter; //设置第一行高度,即标题栏 ((Microsoft.Office.Interop.Excel.Range)sheet.Rows["1:1", System.Type.Missing]).RowHeight = 20; //设置数据行行高度 ((Microsoft.Office.Interop.Excel.Range)sheet.Rows["2:" + gridView.RowCount + 1, System.Type.Missing]).RowHeight = 16; //设置字体大小(10号字体) sheet.Range[sheet.Cells[1, 1], sheet.Cells[gridView.RowCount + 1, gridView.Columns.Count]].Font.Size = 10; //设置单元格边框 Microsoft.Office.Interop.Excel.Range range1 = sheet.Range[sheet.Cells[1, 1], sheet.Cells[gridView.RowCount + 1, gridView.Columns.Count]]; //写标题 for (int row = 1; row <= gridView.Columns.Count; row++) { sheet.Cells[1, row] = gridView.Columns[row - 1].GetTextCaption(); } for (int i = 0; i < gridView.RowCount; i++) { rowIndex++; colIndex = 0; for (int j = 0; j < gridView.Columns.Count; j++) { colIndex++; sheet.Cells[rowIndex, colIndex] = gridView.GetRowCellValue(i, gridView.Columns[j]); } } gridViewIndex++; mBook.SaveAs(sfd.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss); } catch (Exception ex) { throw new Exception(ex.Message); } finally { mBooks.Close(); xlapp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(mBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(mBooks); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp); GC.Collect(); } } else { //return false; } }
public object[] GetSelectedValues(DevExpress.XtraGrid.Views.Grid.GridView view, string fieldName) { int[] selectedRows = view.GetSelectedRows(); object[] result = new object[selectedRows.Length]; for (int i = 0; i < selectedRows.Length; i++) { int rowHandle = selectedRows[i]; if (!gvMain.IsGroupRow(rowHandle)) { result[i] = view.GetRowCellValue(rowHandle, fieldName); } else result[i] = -1; } return result; }