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. Programmatically select multiple cells in DataGridView C# / VB.NET application

Programmatically select multiple cells in DataGridView C# / VB.NET application

Scheduled Pinned Locked Moved C#
csharpquestion
3 Posts 2 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.
  • W Offline
    W Offline
    willempipi
    wrote on last edited by
    #1

    Hi, I'm creating a user control that displays a DataGridView control. I want to be able to programmatically select multiple cells/rows. So i made this function:

    public bool SetSelectedValues(string ColumnName, List<object> CellValues)
    {
    bool FoundRows = false;

    for (int ColumnIndex = 0; ColumnIndex < gridData.Columns.Count; ColumnIndex++)
    {
        if (gridData.Columns\[0\].Name == ColumnName)
        {
            gridData.ClearSelection();
    
            for (int RowIndex = 0; RowIndex < gridData.Rows.Count; RowIndex++)
            {
                foreach (object CellValue in CellValues)
                {
                    if (gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Value.ToString() == CellValue.ToString())
                    {
                        gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Selected = true;
                        gridData.CurrentCell = gridData.Rows\[RowIndex\].Cells\[ColumnIndex\];
    
                        FoundRows = true;
                    }
                }
            }
    
            break;
        }
    }
    
    return FoundRows;
    

    }

    It doesn't work, only the last cell gets selected, because i cannot select multiple cells in the CurrentCell. How can i solve this? Regards, Willem

    D W 2 Replies Last reply
    0
    • W willempipi

      Hi, I'm creating a user control that displays a DataGridView control. I want to be able to programmatically select multiple cells/rows. So i made this function:

      public bool SetSelectedValues(string ColumnName, List<object> CellValues)
      {
      bool FoundRows = false;

      for (int ColumnIndex = 0; ColumnIndex < gridData.Columns.Count; ColumnIndex++)
      {
          if (gridData.Columns\[0\].Name == ColumnName)
          {
              gridData.ClearSelection();
      
              for (int RowIndex = 0; RowIndex < gridData.Rows.Count; RowIndex++)
              {
                  foreach (object CellValue in CellValues)
                  {
                      if (gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Value.ToString() == CellValue.ToString())
                      {
                          gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Selected = true;
                          gridData.CurrentCell = gridData.Rows\[RowIndex\].Cells\[ColumnIndex\];
      
                          FoundRows = true;
                      }
                  }
              }
      
              break;
          }
      }
      
      return FoundRows;
      

      }

      It doesn't work, only the last cell gets selected, because i cannot select multiple cells in the CurrentCell. How can i solve this? Regards, Willem

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      I don't the code here setting MultiSelect as true. See if you have done that. Plus, not sure about CurrentCell thing. Why are you setting it in first place?

      50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

      1 Reply Last reply
      0
      • W willempipi

        Hi, I'm creating a user control that displays a DataGridView control. I want to be able to programmatically select multiple cells/rows. So i made this function:

        public bool SetSelectedValues(string ColumnName, List<object> CellValues)
        {
        bool FoundRows = false;

        for (int ColumnIndex = 0; ColumnIndex < gridData.Columns.Count; ColumnIndex++)
        {
            if (gridData.Columns\[0\].Name == ColumnName)
            {
                gridData.ClearSelection();
        
                for (int RowIndex = 0; RowIndex < gridData.Rows.Count; RowIndex++)
                {
                    foreach (object CellValue in CellValues)
                    {
                        if (gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Value.ToString() == CellValue.ToString())
                        {
                            gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Selected = true;
                            gridData.CurrentCell = gridData.Rows\[RowIndex\].Cells\[ColumnIndex\];
        
                            FoundRows = true;
                        }
                    }
                }
        
                break;
            }
        }
        
        return FoundRows;
        

        }

        It doesn't work, only the last cell gets selected, because i cannot select multiple cells in the CurrentCell. How can i solve this? Regards, Willem

        W Offline
        W Offline
        willempipi
        wrote on last edited by
        #3

        Found the solution myself, the setting of selected cell's is only effective after the datasource is bound to the control. This is not done jet when you are in the Form_Load, so you have to wait for it. This is my solution:

        private string Selection_ColumnName = null;
        private List<object> Selection_CellValues = null;

        public void SetSelectedValues(string ColumnName, List<object> CellValues)
        {
        Selection_ColumnName = ColumnName;
        Selection_CellValues = CellValues;
        gridData.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(gridData_DataBindingComplete);
        }
        private void gridData_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
        bool CellFound = false;

        for (int ColumnIndex = 0; ColumnIndex < gridData.Columns.Count; ColumnIndex++)
        {
            if (gridData.Columns\[0\].Name == Selection\_ColumnName)
            {
                gridData.ClearSelection();
        
                for (int RowIndex = 0; RowIndex < gridData.Rows.Count; RowIndex++)
                {
                    foreach (object CellValue in Selection\_CellValues)
                    {
                        try
                        {
                            if (gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Value.ToString() == CellValue.ToString())
                            {
                                gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Selected = true;
                                if (!CellFound) gridData.CurrentCell = gridData.SelectedCells\[0\];
        
                                CellFound = true;
                            }
                        }
                        catch
                        {
                        }
                    }
                }
        
                break;
            }
        }
        
        gridData.DataBindingComplete -= new DataGridViewBindingCompleteEventHandler(gridData\_DataBindingComplete);
        

        }

        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