using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Stone.Common;
using Stone.Common.Biz;
using System.Drawing;
using System.Data;
namespace Stone.Common
{
public class MyGridViewStyle
{
///
/// 设置LED上显示的DataGrid样式
///
///
public static void SetStyleLED(DataGridView grid)
{
Color bg_color = ColorTranslator.FromHtml("#484848");
grid.BackgroundColor = bg_color;
grid.DefaultCellStyle.BackColor = bg_color;
grid.DefaultCellStyle.ForeColor = Color.White;
grid.DefaultCellStyle.Font = new Font("微软雅黑", 12);
grid.GridColor = Color.Black;
grid.AlternatingRowsDefaultCellStyle.BackColor = ColorTranslator.FromHtml("#666666");
grid.BorderStyle = BorderStyle.None;
grid.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
grid.CellBorderStyle = DataGridViewCellBorderStyle.None;
grid.EnableHeadersVisualStyles = false;
grid.ColumnHeadersDefaultCellStyle.BackColor = Color.Orange;
grid.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
grid.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 12);
grid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
grid.ColumnHeadersHeight = 50;
grid.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
grid.RowHeadersVisible = false;
grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
grid.AllowUserToResizeRows = false;
grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
}
///
/// 显示设置窗口
///
///
public static void Show(DataGridView dgrdView, string TableName)
{
try
{
frmSetDataGridViewColumns frm = new frmSetDataGridViewColumns(dgrdView, TableName);
frm.ShowDialog();
frm.Dispose();
SetDataGrid(dgrdView, TableName);
}
catch (Exception ex)
{
MyMessageBox.ShowErrorMessage(ex.Message);
}
}
public static void SetDataGrid(DataGridView dgrdView, string TableName)
{
for (int i = 0; i < dgrdView.Columns.Count; i++)
{
if(dgrdView.Columns[i].Name.ToUpper() != "ID")
{
string v = MyAppconfigGridViewState.ReadValue("表格配置", TableName + "_v_" + dgrdView.Columns[i].Name);
if (v != "")
{
dgrdView.Columns[i].Visible = Convert.ToBoolean(v);
}
}
}
}
//保存DataGridView的显示状态
public static void SaveGridViewState(DataGridView dgv, string TableName)
{
try
{
for (int i = 0; i < dgv.Columns.Count; i++)
{
MyAppconfigGridViewState.WriteValue("表格配置", TableName + "_w_" + dgv.Columns[i].Name, dgv.Columns[i].Width.ToString());
MyAppconfigGridViewState.WriteValue("表格配置", TableName + "_d_" + dgv.Columns[i].Name, dgv.Columns[i].DisplayIndex.ToString());
MyAppconfigGridViewState.WriteValue("表格配置", TableName + "_v_" + dgv.Columns[i].Name, dgv.Columns[i].Visible.ToString());
}
}
catch
{
}
}
//获取DataGridView的显示状态
public static void GetGridViewState(DataGridView dgv, string TableName)
{
try
{
for (int i = 0; i < dgv.Columns.Count; i++)
{
string w = MyAppconfigGridViewState.ReadValue("表格配置", TableName + "_w_" + dgv.Columns[i].Name);
string d = MyAppconfigGridViewState.ReadValue("表格配置", TableName + "_d_" + dgv.Columns[i].Name);
string v = MyAppconfigGridViewState.ReadValue("表格配置", TableName + "_v_" + dgv.Columns[i].Name);
if (w.Trim() != "") dgv.Columns[i].Width = Convert.ToInt32(w);
if (d.Trim() != "") dgv.Columns[i].DisplayIndex = Convert.ToInt32(d);
if (v.Trim() != "") dgv.Columns[i].Visible = Convert.ToBoolean(v);
}
}
catch { }
}
//public static void SetDataGridRowNumberNew(DataGridView dgv)
//{
/*
DataTable dtData = (DataTable)dgv.DataSource;
dtData.Columns.Add("Grid_SN", typeof(System.Int32));
dtData.Columns["Grid_SN"].SetOrdinal(1);
for (int i = 0; i < dtData.Rows.Count; i++)
{
dtData.Rows[i]["Grid_SN"] = i + 1;
}
//dgv.Columns["Grid_SN"].DisplayIndex = 0;
dgv.Columns["Grid_SN"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dgv.Columns["Grid_SN"].HeaderText = "序号";
//dgv.Columns["Grid_SN"].Width = 60;
*/
//}
public static void SetDataGridRowNumber(DataGridView dgv)
{
//dgv.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
//dgv.RowsAdded += new DataGridViewRowsAddedEventHandler(dgv_RowsAdded);
int line = dgv.Rows.Count;
Size size = TextRenderer.MeasureText(line.ToString(), dgv.RowHeadersDefaultCellStyle.Font);
dgv.RowHeadersWidth = size.Width + 30;
dgv.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dgv_RowPostPaint);
}
static void dgv_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
SolidBrush solidBrush = new SolidBrush(((DataGridView)sender).RowHeadersDefaultCellStyle.ForeColor);
e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, solidBrush,
new Rectangle(e.RowBounds.X+15, e.RowBounds.Y+7, ((DataGridView)sender).RowHeadersWidth,e.RowBounds.Height)
);
}
public static void SetDataGridRowNumber(DataGridView dgv, int RowHeadersWidth )
{
dgv.RowHeadersWidth = RowHeadersWidth;
dgv.RowsAdded += new DataGridViewRowsAddedEventHandler(dgv_RowsAdded);
}
static void dgv_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
for (int i = 0; i < e.RowCount; i++)
{
((DataGridView)sender).Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
((DataGridView)sender).Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString();
}
//for (int i = e.RowIndex + e.RowCount; i < ((DataGridView)sender).Rows.Count; i++)
//{
// ((DataGridView)sender).Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
// ((DataGridView)sender).Rows[i].HeaderCell.Value = (i + 1).ToString();
//}
}
public static void SetDataGridMenuCommon(DataGridView dgv)
{
try
{
ContextMenuStrip cmenu = new ContextMenuStrip();
dgv.ContextMenuStrip = cmenu;
ToolStripMenuItem tItem1 = new ToolStripMenuItem("复制单元格"); tItem1.Tag = dgv;
ToolStripMenuItem tItem2 = new ToolStripMenuItem("复制当前行"); tItem2.Tag = dgv;
ToolStripMenuItem tItem3 = new ToolStripMenuItem("复制所有行"); tItem3.Tag = dgv;
cmenu.Items.Add(tItem1);
cmenu.Items.Add(tItem2);
cmenu.Items.Add(tItem3);
tItem1.Click += new EventHandler(tItem1_Click);
tItem2.Click += new EventHandler(tItem2_Click);
tItem3.Click += new EventHandler(tItem3_Click);
}
catch (Exception ex)
{
MyMessageBox.ShowErrorMessage(ex.Message);
}
}
static void tItem1_Click(object sender, EventArgs e)
{
if (((DataGridView)((ToolStripMenuItem)sender).Tag).Rows.Count > 0)
{
if (((DataGridView)((ToolStripMenuItem)sender).Tag).CurrentCell.Value != null)
{
Clipboard.SetText(((DataGridView)((ToolStripMenuItem)sender).Tag).CurrentCell.Value.ToString());
}
}
}
static void tItem2_Click(object sender, EventArgs e)
{
if (((DataGridView)((ToolStripMenuItem)sender).Tag).Rows.Count > 0)
{
if (((DataGridView)((ToolStripMenuItem)sender).Tag).CurrentRow != null)
{
string text = "";
foreach (DataGridViewCell cell in ((DataGridView)((ToolStripMenuItem)sender).Tag).CurrentRow.Cells)
{
if (cell.Value != null)
{
text += cell.Value.ToString() + "\t";
}
}
Clipboard.SetText(text);
}
}
}
static void tItem3_Click(object sender, EventArgs e)
{
if (((DataGridView)((ToolStripMenuItem)sender).Tag).Rows.Count > 0)
{
string text = "";
foreach(DataGridViewRow row in ((DataGridView)((ToolStripMenuItem)sender).Tag).Rows)
{
for(int i=0; i