专注

导航

DataGridView的一些操作

   1:          /// <summary>
   2:          /// 设定DataGridView中的表头文字-已经绑定了数据,只是添加一个表头(汉字)
   3:          /// </summary>
   4:          /// <param name="str">表头字符串,一“,”分割</param>
   5:          /// <param name="DGV">dataGridView</param>
   6:          public static void BindDataGridViewHead(string str, DataGridView DGV)
   7:          {
   8:              if (str != string.Empty)
   9:              {
  10:                  string[] temp = str.Split(',');
  11:                  int i = 0;
  12:                  foreach (string strHead in temp)
  13:                  {
  14:                      DGV.Columns[i].HeaderText = strHead;
  15:                      i++;
  16:                  }
  17:              }
  18:          }
  19:   
  20:   
  21:          /// <summary>
  22:          /// 绑定一个空的DataGridView(没有数据)
  23:          /// </summary>
  24:          /// <param name="str">表头字符串,一“,”分割</param>
  25:          /// <param name="DGV"></param>
  26:          public static void BindDataGridViewHeadNull(string str, DataGridView DGV)
  27:          {
  28:              if (str != string.Empty)
  29:              {
  30:                  string[] temp = str.Split(',');
  31:                  DataTable newTable = new DataTable();
  32:                  foreach (string strHead in temp)
  33:                  {
  34:                      newTable.Columns.Add(strHead, typeof(string));
  35:                  }
  36:                  DGV.DataSource = newTable;
  37:              }
  38:          }

 

 

   1:          /// <summary>
   2:          /// DataGridView的行号显示
   3:         /// </summary>
   4:         /// <param name="GV"></param>
   5:         /// <param name="e"></param>
   6:          public static void DisDataGridViewRowsNum(DataGridView GV, DataGridViewRowPostPaintEventArgs e)
   7:          {
   8:              try
   9:              {
  10:                  Rectangle rec = new Rectangle(e.RowBounds.Location.X, Convert.ToInt32(e.RowBounds.Location.Y + (e.RowBounds.Height - GV.RowHeadersDefaultCellStyle.Font.Size) / 2), GV.RowHeadersWidth - 4, e.RowBounds.Height);
  11:                  TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), GV.RowHeadersDefaultCellStyle.Font, rec, GV.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.Right);
  12:              }
  13:              catch (Exception)
  14:              {
  15:                  throw;
  16:              }
  17:          }
  18:   
  19:   
  20:           /// <summary>
  21:          /// 将DataGridView控件中数据导出到Excel
  22:          /// </summary>
  23:          /// <param name="gridView">DataGridView对象</param>
  24:          /// <param name="isShowExcle">是否显示Excel界面</param>
  25:          /// <returns></returns>
  26:          public static bool ExportDataGridview(DataGridView gridView, bool isShowExcle)
  27:          {
  28:              if (gridView.Rows.Count == 0)
  29:                  return false;
  30:              //建立Excel对象
  31:              Excel.Application excel = new Excel.Application();
  32:              excel.Application.Workbooks.Add(true);
  33:              excel.Visible = isShowExcle;
  34:              //生成字段名称
  35:              for (int i = 0; i < gridView.ColumnCount; i++)
  36:              {
  37:                  excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText;
  38:              }
  39:              //填充数据
  40:              for (int i = 0; i < gridView.RowCount; i++)
  41:              {
  42:                  for (int j = 0; j < gridView.ColumnCount; j++)
  43:                  {
  44:                      if (gridView[j, i].ValueType == typeof(string))
  45:                      {
  46:                          excel.Cells[i + 2, j + 1] = "'" + gridView[j, i].Value.ToString();
  47:                      }
  48:                      else
  49:                      {
  50:                          excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
  51:                      }
  52:                  }
  53:              }
  54:              return true;
  55:          }

posted on 2011-11-13 11:35  陈啊M  阅读(179)  评论(0编辑  收藏  举报