Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

This is Form1 Code
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Datagridview__Cell_Paint
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            dataGridView1.RowCount = 5;
        }
        

        private void addColorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
             if (e.RowIndex != -1 && e.ColumnIndex != -1)
            {
            if (e.Button == MouseButtons.Right)
                {
                DataGridViewRow clickedRow = (sender as DataGridView).Rows[e.RowIndex];
                if (!clickedRow.Selected)
                    dataGridView1.CurrentCell = clickedRow.Cells[e.ColumnIndex];
                }
            }
        }
    }
}



Form2 code Here


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Datagridview__Cell_Paint
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }        
        private void panel4_Paint(object sender, PaintEventArgs e)
        {
            Pen graphPen = new Pen(Color.White, 20);
            PointF pt1D = new PointF();
            PointF pt2D = new PointF();
            pt1D.X = 5;
            pt1D.Y = 20;
            pt2D.X = 175;
            pt2D.Y = 20;

            e.Graphics.DrawLine(graphPen, pt1D, pt2D);
            
        }

        private void panel4_Click(object sender, EventArgs e)
        {
            System.Drawing.Graphics graphicsObj;
            graphicsObj = panel2.CreateGraphics();
            Pen graphPen = new Pen(panel1.BackColor, 20);
            graphicsObj.DrawLine(graphPen, 5, 20, 175, 20);
           
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ColorDialog cd = new ColorDialog();
                if (cd.ShowDialog() == DialogResult.OK)
                {
                    this.panel1.BackColor = cd.Color;
                }
            }
        }
    }
}

if i right click on datagridview cell form2 is open.i selected color using right click on panel1 eg.red color.then i click on panel2 panel1 color is shown panel2 red color(panel2 iam using paint and click event graphics Drawline method

after this if i click Add to form1 button click it shold be shown panel2 DrawLine red color show to form1 datagridview cell.

also i attached my project file also.plz ref and help us.
http://www.mediafire.com/download/8bdn8asb82ht12r/Datagridview__Cell_Paint.rar

Rgds
Posted

1 solution

You are doing it wrong. You create an instance of Graphics and draw using it, but nothing supports this graphic to persistently show in the control. Nearest invalidation, by whatever reason, will wipe it out.

Here is what you should use: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellpainting%28v=vs.110%29.aspx[^].

Note that you should not create an instance of Graphics but use the one passed to your handler in event arguments.

I explained some background here: What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

—SA
 
Share this answer
 
Comments
cadsolution 30-Sep-14 3:43am    
Dear Sergey

Thanks.
Please Share me the code actually what i need .my purpose is i want to draw color on form1 datagrid view cell from form2 panel seclected color.
share me the code please.
Sergey Alexandrovich Kryukov 30-Sep-14 9:54am    
Please see the documentation and try to do it yourself. If you face some problem, please ask your questions.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900