Christopher
Christopher

Reputation: 788

Saving out of scope C#

I am trying to create a program where a user can color cells inside of a datagridview, then save the datagridview as an image. However, I am running into a problem with scope when I generate my datagridview the way I need to.

I can't think of a good (and quick) way of restructuring this in a way that makes sense. How can I avoid my datagridview from going out of scope in this context?

Thanks!

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);
    }

}

}

Upvotes: 0

Views: 64

Answers (1)

Robert Moskal
Robert Moskal

Reputation: 22553

It should be dataGridView1 and not DataGridView1 in the savegviewImg() method, no? That's how you refer to it everywhere else.

Upvotes: 1

Related Questions