Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. change picture of image controll in gridvie in windows application???? [not solved yet...]

change picture of image controll in gridvie in windows application???? [not solved yet...]

Scheduled Pinned Locked Moved C#
helpquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mr mohsen
    wrote on last edited by
    #1

    hi friends i have a datagridview and i add a column to this datagrid that has a picture(start symbol) i want to write a code that when a user click on the picture in datagridview the picture of that control has been changed (stop symbol). please tell me a solution and say me what code are usefull for this action

    nobody help you... you have to help you yourself and this is success way.

    modified on Thursday, October 8, 2009 1:32 PM

    A H 2 Replies Last reply
    0
    • M mr mohsen

      hi friends i have a datagridview and i add a column to this datagrid that has a picture(start symbol) i want to write a code that when a user click on the picture in datagridview the picture of that control has been changed (stop symbol). please tell me a solution and say me what code are usefull for this action

      nobody help you... you have to help you yourself and this is success way.

      modified on Thursday, October 8, 2009 1:32 PM

      A Offline
      A Offline
      Atif Ali Bhatti
      wrote on last edited by
      #2

      hi, its simple... add following html in the gridview column. <a href="javascript:void(0);" onclick="ChangePic(this);"><img src="xyz.jpg"/></a> now add a add following javascript to your page: function ChangePic(sender) { sender.childNodes[0].src="abc.jpg"; } there are many more ways to do this. Hope It solves ur problem... Regards Atif Ali Bhatti.

      M 1 Reply Last reply
      0
      • A Atif Ali Bhatti

        hi, its simple... add following html in the gridview column. <a href="javascript:void(0);" onclick="ChangePic(this);"><img src="xyz.jpg"/></a> now add a add following javascript to your page: function ChangePic(sender) { sender.childNodes[0].src="abc.jpg"; } there are many more ways to do this. Hope It solves ur problem... Regards Atif Ali Bhatti.

        M Offline
        M Offline
        mr mohsen
        wrote on last edited by
        #3

        i spoke about a windows application not web application my friend

        nobody help you... you have to help you yourself and this is success way.

        1 Reply Last reply
        0
        • M mr mohsen

          hi friends i have a datagridview and i add a column to this datagrid that has a picture(start symbol) i want to write a code that when a user click on the picture in datagridview the picture of that control has been changed (stop symbol). please tell me a solution and say me what code are usefull for this action

          nobody help you... you have to help you yourself and this is success way.

          modified on Thursday, October 8, 2009 1:32 PM

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          Is the column a bound column, or an unbound column.

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          M 1 Reply Last reply
          0
          • H Henry Minute

            Is the column a bound column, or an unbound column.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            M Offline
            M Offline
            mr mohsen
            wrote on last edited by
            #5

            no that is not a bound coloumn the column is used for showing to user the step of action such as start(show start symbol) or pause(show pause symbol) or stop(show stop symbol)..

            nobody help you... you have to help you yourself and this is success way.

            H 1 Reply Last reply
            0
            • M mr mohsen

              no that is not a bound coloumn the column is used for showing to user the step of action such as start(show start symbol) or pause(show pause symbol) or stop(show stop symbol)..

              nobody help you... you have to help you yourself and this is success way.

              H Offline
              H Offline
              Henry Minute
              wrote on last edited by
              #6

              Then handle the CellClick event of the DataGridView:

              private Bitmap start = new Bitmap("LocationForImage");
              private Bitmap stop = new Bitmap("LocationForImage");
              private void dataGridView1\_CellClick(object sender, DataGridViewCellEventArgs e)
              {
                  // Try to load it as a DataGridViewImageCell
                  DataGridViewImageCell img = dataGridView1.Rows\[e.RowIndex\].Cells\[e.ColumnIndex\] as DataGridViewImageCell
                  // If that works, we have the right cell
                  if (img != null)
                  {
                      if ((bool)img.Tag)
                      {
                          // if it is true then we are started, so stop
                          img.Value = stop;
                          img.Tag = false;
                          // HERE DO ANYTHING ELSE REQUIRED FOR STOPPING e.g. this.timer1.Stop;
                      }
                      else
                      {
                          // if it is false then we are stopped, so start
                          img.Value = start;
                          img.Tag = true;
                          // HERE DO ANYTHING ELSE REQUIRED FOR STARTING e.g. this.timer1.Start;
                      }
                  }
              }
              

              This assumes that there is only one Image column. As you initially load the data into the image cells, you must set the Tag property to either true (start image used) or false (stop image used) I hope that this helps. :)

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              M 1 Reply Last reply
              0
              • H Henry Minute

                Then handle the CellClick event of the DataGridView:

                private Bitmap start = new Bitmap("LocationForImage");
                private Bitmap stop = new Bitmap("LocationForImage");
                private void dataGridView1\_CellClick(object sender, DataGridViewCellEventArgs e)
                {
                    // Try to load it as a DataGridViewImageCell
                    DataGridViewImageCell img = dataGridView1.Rows\[e.RowIndex\].Cells\[e.ColumnIndex\] as DataGridViewImageCell
                    // If that works, we have the right cell
                    if (img != null)
                    {
                        if ((bool)img.Tag)
                        {
                            // if it is true then we are started, so stop
                            img.Value = stop;
                            img.Tag = false;
                            // HERE DO ANYTHING ELSE REQUIRED FOR STOPPING e.g. this.timer1.Stop;
                        }
                        else
                        {
                            // if it is false then we are stopped, so start
                            img.Value = start;
                            img.Tag = true;
                            // HERE DO ANYTHING ELSE REQUIRED FOR STARTING e.g. this.timer1.Start;
                        }
                    }
                }
                

                This assumes that there is only one Image column. As you initially load the data into the image cells, you must set the Tag property to either true (start image used) or false (stop image used) I hope that this helps. :)

                Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                M Offline
                M Offline
                mr mohsen
                wrote on last edited by
                #7

                thank you my friend for your reply your code run correctly but without any result and the image has not changed.!!!!

                nobody help you... you have to help you yourself and this is success way.

                H 1 Reply Last reply
                0
                • M mr mohsen

                  thank you my friend for your reply your code run correctly but without any result and the image has not changed.!!!!

                  nobody help you... you have to help you yourself and this is success way.

                  H Offline
                  H Offline
                  Henry Minute
                  wrote on last edited by
                  #8

                  Well I hadn't tested it although there was no reason that it shouldn't work, so I built a test app and it works exactly as I thought. Here is the code for the entire form:

                  public partial class ImageColumnTestForm : Form
                  {
                  	private Bitmap start = ImageCellTest.Properties.Resources.START; // I am getting the images from the apps resources
                  	private Bitmap stop = ImageCellTest.Properties.Resources.STOP;   // 'ImageCellTest' is the Namespace that I used.
                  
                  	public ImageColumnTestForm()
                  	{
                  		InitializeComponent();
                  	}
                  
                  	private void ImageColumnTestForm\_Load(object sender, EventArgs e)
                  	{
                  		DataGridViewRow newRow = null;
                  		DataGridViewTextBoxCell textCell = null;
                  		DataGridViewImageCell imgCell = null;
                  
                  		for (int i = 0; i < 5; i++)
                  		{
                  			newRow = new DataGridViewRow();
                  			textCell = new DataGridViewTextBoxCell();
                  			newRow.Cells.Add(textCell);
                  			imgCell = new DataGridViewImageCell();
                  			newRow.Cells.Add(imgCell);
                  			textCell.Value = "Text " + i.ToString();
                  			imgCell.Value = start;
                  			imgCell.Tag = true;
                  			this.dataGridView1.Rows.Add(newRow);
                  		}
                  	}
                  
                  	private void dataGridView1\_CellClick(object sender, DataGridViewCellEventArgs e)
                  	{
                  		// Try to load it as a DataGridViewImageCell
                  		DataGridViewImageCell img = dataGridView1.Rows\[e.RowIndex\].Cells\[e.ColumnIndex\] as DataGridViewImageCell;
                  		// If that works, we have the right cell
                  		if (img != null)
                  		{
                  			if ((bool)img.Tag)
                  			{
                  				// if it is true then we are started, so stop
                  				img.Value = stop;
                  				img.Tag = false;
                  				// HERE DO ANYTHING ELSE REQUIRED FOR STOPPING e.g. this.timer1.Stop;
                  			}
                  			else
                  			{
                  				// if it is false then we are stopped, so start
                  				img.Value = start;
                  				img.Tag = true;
                  				// HERE DO ANYTHING ELSE REQUIRED FOR STARTING e.g. this.timer1.Start;
                  			}
                  		}
                  	}
                  }
                  

                  As you can see I simply copied the CellClick code from my previous reply to you and all that I changed was to add the semi-colon to the first line, as I am sure that you did. :-O This is just a standard form with a datagridview on it and it works just fine.

                  Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                  M 1 Reply Last reply
                  0
                  • H Henry Minute

                    Well I hadn't tested it although there was no reason that it shouldn't work, so I built a test app and it works exactly as I thought. Here is the code for the entire form:

                    public partial class ImageColumnTestForm : Form
                    {
                    	private Bitmap start = ImageCellTest.Properties.Resources.START; // I am getting the images from the apps resources
                    	private Bitmap stop = ImageCellTest.Properties.Resources.STOP;   // 'ImageCellTest' is the Namespace that I used.
                    
                    	public ImageColumnTestForm()
                    	{
                    		InitializeComponent();
                    	}
                    
                    	private void ImageColumnTestForm\_Load(object sender, EventArgs e)
                    	{
                    		DataGridViewRow newRow = null;
                    		DataGridViewTextBoxCell textCell = null;
                    		DataGridViewImageCell imgCell = null;
                    
                    		for (int i = 0; i < 5; i++)
                    		{
                    			newRow = new DataGridViewRow();
                    			textCell = new DataGridViewTextBoxCell();
                    			newRow.Cells.Add(textCell);
                    			imgCell = new DataGridViewImageCell();
                    			newRow.Cells.Add(imgCell);
                    			textCell.Value = "Text " + i.ToString();
                    			imgCell.Value = start;
                    			imgCell.Tag = true;
                    			this.dataGridView1.Rows.Add(newRow);
                    		}
                    	}
                    
                    	private void dataGridView1\_CellClick(object sender, DataGridViewCellEventArgs e)
                    	{
                    		// Try to load it as a DataGridViewImageCell
                    		DataGridViewImageCell img = dataGridView1.Rows\[e.RowIndex\].Cells\[e.ColumnIndex\] as DataGridViewImageCell;
                    		// If that works, we have the right cell
                    		if (img != null)
                    		{
                    			if ((bool)img.Tag)
                    			{
                    				// if it is true then we are started, so stop
                    				img.Value = stop;
                    				img.Tag = false;
                    				// HERE DO ANYTHING ELSE REQUIRED FOR STOPPING e.g. this.timer1.Stop;
                    			}
                    			else
                    			{
                    				// if it is false then we are stopped, so start
                    				img.Value = start;
                    				img.Tag = true;
                    				// HERE DO ANYTHING ELSE REQUIRED FOR STARTING e.g. this.timer1.Start;
                    			}
                    		}
                    	}
                    }
                    

                    As you can see I simply copied the CellClick code from my previous reply to you and all that I changed was to add the semi-colon to the first line, as I am sure that you did. :-O This is just a standard form with a datagridview on it and it works just fine.

                    Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                    M Offline
                    M Offline
                    mr mohsen
                    wrote on last edited by
                    #9

                    thank you man you see i try your code again in new project and work correctly then try to affect your code in main project but not work at all so i decide to create my datagridview and finally work correctly. i think that the problem was on visual studio not on your code or previose code that i used. thank you again.

                    nobody help you... you have to help you yourself and this is success way.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups