none
how to properly function IF condition upon button click with "e.columnindex" RRS feed

  • Question

  • I have a script here that filters for active and inactive users in a datagridview based from an image. The code works during CellValueChanged event, but now i want the script to work during a button click.  

    private void dg_vw_actve_doc_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == 1 && !isInit)
                {
                    var valueCell = dg_vw_actve_doc.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    var imgCell = dg_vw_actve_doc.Rows[e.RowIndex].Cells[e.ColumnIndex + 1]; // Whatever index your colImg is
                                                                                             // char firstCharacterInCell = valueCell.Value.ToString()[1];
                                                                                             //if (firstCharacterInCell == 'A')
                    if (int.Parse(valueCell.Value.ToString()) == 1)
                        imgCell.Value = Properties.Resources.active;
                    else
                        imgCell.Value = Properties.Resources.inactive;
                }
            }

    Friday, August 2, 2019 6:13 AM

All replies

  • Greetings Dualshock03.

    If you want to work on the cells the user has selected, you could do it something like this.

          private void button1_Click(object sender, EventArgs e)
          {
             foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
             {
                int column = cell.ColumnIndex;
                int row = cell.RowIndex;
    
                // Do your thing with the column and row numbers here.
             }
          }

    Friday, August 2, 2019 6:40 AM
  • i just want to load the datagridview with the image already set based  on variable valueCell  which will automatically set the images for active and inactive on the first column of the datagridview by a button click

    sample:

    Datagridview loaded data upon button click::

    ---------------------------------------------------------

        Status        |       flag           |   Doctor Name   |

    ---------------------------------------------------------

    Active Image            1                  Juan Dela Cruz

    Inactive Image         0                  Thomas Cruz

    ---------------------------------------------------------

    Friday, August 2, 2019 8:51 AM
  • thanks for the suggestion
    Friday, August 2, 2019 9:41 AM
  • Good Day Sir!

    I followed your code sample and it works but i have a another problem with regards to the dataGridView1.SelectedCells. I want the button to load all the status images not from during selectedCells but during loading of datagridview after button click

    I'm only getting 1 image based from the default selected cell

    Saturday, August 3, 2019 8:39 AM
  • Hello,

    See if this would work for you.

    Each column was created in the form designer, status column type is DataGridViewImageColumn

    using System;
    using System.Data;
    using System.Windows.Forms;
    
    namespace DataGridViewImage
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Shown += Form1_Shown;
            }
            private readonly DataTable _doctorDataTable = new DataTable();
            private void Form1_Shown(object sender, EventArgs e)
            {
                dataGridView1.AutoGenerateColumns = false;
                
                _doctorDataTable.Columns.Add(new DataColumn()
                    { ColumnName = "Status", DataType = typeof(int)});
    
                _doctorDataTable.Columns.Add(new DataColumn()
                    { ColumnName = "DoctorName", DataType = typeof(string) });
    
                _doctorDataTable.Rows.Add(1, "Juan Dela Cruz");
                _doctorDataTable.Rows.Add(0, "Thomas Cruz");
                _doctorDataTable.Rows.Add(1, "Karen Payne");
                dataGridView1.DataSource = _doctorDataTable;
                dataGridView1.CellFormatting += DataGridView1_CellFormatting;
            }
            private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (dataGridView1.Columns[e.ColumnIndex].Name != "StatusColumn") return;
    
                e.Value = _doctorDataTable.Rows[e.RowIndex].Field<int>("Status") == 1 ? 
                    Properties.Resources.Active : 
                    Properties.Resources.Inactive;
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, August 3, 2019 10:30 AM
    Moderator
  • You can loop through all the cells like so.

             for (int rowIndex = 0; rowIndex < dataGridView1.RowCount; rowIndex++)
             {
                for (int columnIndex = 0; columnIndex < dataGridView1.ColumnCount; columnIndex++)
                {
                   // Your code for rowIndex and columnIndex goes here.
                }
             }

    Monday, August 5, 2019 1:29 AM
  • I have a problem during sorting the column, the whole Status column messed up and not in proper order correspond to my flag column. 

    sample.

                 ("flag" and "Doctors Name" comes from database query column)

                                |-------------------------|                           

    ---------------------------------------------------------

        Status         |       flag           |   Doctor Name   |

    ---------------------------------------------------------

    Inactive Image            1                  Juan Dela Cruz

    Inactive Image            0                  Thomas Cruz

    Inactive Image            0                  Karen Payne

    Active Image               0                  Joshua Sanders

    ---------------------------------------------------------

    Wednesday, August 7, 2019 10:25 AM
  • Hello,

    I don't have enough time to do a full example so the images are hard coded where in your program iterate the DataTable rows collection, check the status column value, is it 1 use the Active image, if 0 use the InActive image. The first column in the DataGridView name is StatusColumn with the DataPropertyName set to StatusImage which matches the DataTable column.

    using System;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Shown += Form1_Shown;
            }
            private readonly DataTable _doctorDataTable = new DataTable();
            public static byte[] ImageToByte(Image img)
            {
                var converter = new ImageConverter();
                return (byte[])converter.ConvertTo(img, typeof(byte[]));
            }
            private void Form1_Shown(object sender, EventArgs e)
            {
                dataGridView1.AutoGenerateColumns = false;
                _doctorDataTable.Columns.Add(new DataColumn()
                    { ColumnName = "StatusImage", DataType = typeof(byte[]) });
    
                _doctorDataTable.Columns.Add(new DataColumn()
                    { ColumnName = "Status", DataType = typeof(int) });
    
                _doctorDataTable.Columns.Add(new DataColumn()
                    { ColumnName = "DoctorName", DataType = typeof(string) });
    
                _doctorDataTable.Rows.Add(
                    ImageToByte(Properties.Resources.Inactive),0, "Juan Dela Cruz");
    
                _doctorDataTable.Rows.Add(
                    ImageToByte(Properties.Resources.Inactive),0, "Thomas Cruz");
    
                _doctorDataTable.Rows.Add(
                    ImageToByte(Properties.Resources.Active),1, "Karen Payne");
    
                _doctorDataTable.Rows.Add(
                    ImageToByte(Properties.Resources.Active),1, "Jim Adams");
    
                dataGridView1.DataSource = _doctorDataTable;
    
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, August 7, 2019 12:38 PM
    Moderator