我正在尝试创建一个程序,用户可以在其中为数据网格视图中的单元格着色,然后将datagridview保存为图像。但是,当我以我需要的方式生成datagridview时,我遇到了范围问题。
我无法以一种有意义的方式思考一种良好(快速)的重组方式。如何在这种情况下避免我的datagridview超出范围?
谢谢!
public partial class Form2 : Form
{
bool erasing = false;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
dataGridView1.ColumnCount = Properties.Settings.Default.Width;
dataGridView1.RowCount = Properties.Settings.Default.Height;
dataGridView1.RowHeadersVisible = false;
dataGridView1.ColumnHeadersVisible = false;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dataGridView1.AutoSize = true;
dataGridView1.ClearSelection();
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black;
dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
}
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (erasing)
{
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White;
dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
}
else
{
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black;
dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Black;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
erasing = true;
}
else
{
erasing = false;
}
}
private void savegviewImg()
{
Bitmap bmap = new Bitmap(DataGridView1.Bounds.Width, DataGridView1.Bounds.Height);
DataGridView1.DrawToBitmap(bmap, new Rectangle(1, 1, DataGridView1.Width, DataGridView1.Height));
bmap.Save("C:\\HelpMehStackOFlowYoureMyOnlyHope.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
答案 0 :(得分:1)
在savegviewImg()方法中应该是dataGridView1而不是DataGridView1,不是吗?这就是你在其他地方引用它的方式。