MSDN > Home page del forum > Visual C++ General > can't figure out how to add items to a datagridview
Formula una domandaFormula una domanda
 

Con rispostacan't figure out how to add items to a datagridview

  • giovedì 2 luglio 2009 6.42cheatcountry Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    I know how to add rows to a datagridview in VB and C# but I can't seem to figure out how to do it in C++. I have a textbox that the user fills in with their name and then its supposed to add the name to a row in a datagridview below but I can't figure out what the code is for adding items to a datagridview in C++. If you have an example please share

Risposte

  • giovedì 2 luglio 2009 15.40Nishant SivakumarModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    It should be pretty much the same as you'd do with C# - except for syntactic changes. Where exactly do you have a problem?
    http://blog.voidnish.com
    • Contrassegnato come rispostacheatcountry sabato 4 luglio 2009 3.31
    •  
  • giovedì 2 luglio 2009 20.52jinzai Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con rispostaContiene codice
    Its the same method in C++.

    ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system.windows.forms/html/e940b01d-aea2-966f-5221-ecf4a7d7424b.htm

    That link takes you to examples for DataGridView.Rows Here is the C++ snippet from there, which uses that same method:

       void InitializeDataGridView()
       {
          this->Size = System::Drawing::Size( 600, 600 );
          dataGridView1->Size = System::Drawing::Size( 450, 400 );
    
          // Create an unbound DataGridView by declaring a column count.
          dataGridView1->ColumnCount = 4;
          dataGridView1->ColumnHeadersVisible = true;
    
          // Set the column header style.
          DataGridViewCellStyle ^ columnHeaderStyle = gcnew DataGridViewCellStyle;
          columnHeaderStyle->BackColor = Color::Aqua;
          columnHeaderStyle->Font = gcnew System::Drawing::Font( "Verdana",10,FontStyle::Bold );
          dataGridView1->ColumnHeadersDefaultCellStyle = columnHeaderStyle;
    
          // Set the column header names.
          dataGridView1->Columns[ 0 ]->Name = "Recipe";
          dataGridView1->Columns[ 1 ]->Name = "Category";
          dataGridView1->Columns[ 2 ]->Name = "Main Ingredients";
          dataGridView1->Columns[ 3 ]->Name = "Rating";
    
          // Populate the rows.
          array<String^>^row1 = gcnew array<String^>{
             "Meatloaf","Main Dish","ground beef","**"
          };
          array<String^>^row2 = gcnew array<String^>{
             "Key Lime Pie","Dessert","lime juice, evaporated milk","****"
          };
          array<String^>^row3 = gcnew array<String^>{
             "Orange-Salsa Pork Chops","Main Dish","pork chops, salsa, orange juice","****"
          };
          array<String^>^row4 = gcnew array<String^>{
             "Black Bean and Rice Salad","Salad","black beans, brown rice","****"
          };
          array<String^>^row5 = gcnew array<String^>{
             "Chocolate Cheesecake","Dessert","cream cheese","***"
          };
          array<String^>^row6 = gcnew array<String^>{
             "Black Bean Dip","Appetizer","black beans, sour cream","***"
          };
          array<Object^>^rows = {row1,row2,row3,row4,row5,row6};
          System::Collections::IEnumerator^ myEnum = rows->GetEnumerator();
          while ( myEnum->MoveNext() )
          {
             array<String^>^rowArray = safe_cast<array<String^>^>(myEnum->Current);
             dataGridView1->Rows->Add( rowArray );
          }
       }
    
       void Button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
       {
          // Resize the height of the column headers. 
          dataGridView1->AutoResizeColumnHeadersHeight();
    
          // Resize all the row heights to fit the contents of all non-header cells.
          dataGridView1->AutoResizeRows(
                DataGridViewAutoSizeRowsMode::AllCellsExceptHeaders);
       }
    
       void InitializeContextMenu()
       {
          // Create the menu item.
          MenuItem^ getRecipe = gcnew MenuItem( "Search for recipe",gcnew System::EventHandler( this, &Form1::OnMenuClick ) );
    
          // Add the menu item to the shortcut menu.
          System::Windows::Forms::ContextMenuStrip^ recipeMenu = gcnew System::Windows::Forms::ContextMenuStrip();
    
          // Set the shortcut menu for the first column.
          dataGridView1->Columns[ 0 ]->ContextMenuStrip = recipeMenu;
       }
    
       void OnMenuClick( Object^ /*sender*/, System::EventArgs^ /*e*/ )
       {
          if ( dataGridView1->CurrentCell != nullptr )
          {
             //Retrieve the recipe name.
             String^ recipeName = dynamic_cast<String^>(dataGridView1->CurrentCell->Value);
    
             //Search for the recipe.
             System::Diagnostics::Process::Start( String::Format( "http://search.msn.com/results.aspx?q={0}", recipeName ), nullptr );
          }
       }
    
    private:
    • Contrassegnato come rispostacheatcountry sabato 4 luglio 2009 3.32
    • Modificatojinzai giovedì 2 luglio 2009 20.53emphasize method
    •  

Tutte le risposte

  • giovedì 2 luglio 2009 15.40Nishant SivakumarModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    It should be pretty much the same as you'd do with C# - except for syntactic changes. Where exactly do you have a problem?
    http://blog.voidnish.com
    • Contrassegnato come rispostacheatcountry sabato 4 luglio 2009 3.31
    •  
  • giovedì 2 luglio 2009 20.32cheatcountry Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    In C# you do datagridview.Rows.Add(stuff here) but with C++ there is no method for Rows or a method for adding that I have been able to find.
  • giovedì 2 luglio 2009 20.52jinzai Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con rispostaContiene codice
    Its the same method in C++.

    ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system.windows.forms/html/e940b01d-aea2-966f-5221-ecf4a7d7424b.htm

    That link takes you to examples for DataGridView.Rows Here is the C++ snippet from there, which uses that same method:

       void InitializeDataGridView()
       {
          this->Size = System::Drawing::Size( 600, 600 );
          dataGridView1->Size = System::Drawing::Size( 450, 400 );
    
          // Create an unbound DataGridView by declaring a column count.
          dataGridView1->ColumnCount = 4;
          dataGridView1->ColumnHeadersVisible = true;
    
          // Set the column header style.
          DataGridViewCellStyle ^ columnHeaderStyle = gcnew DataGridViewCellStyle;
          columnHeaderStyle->BackColor = Color::Aqua;
          columnHeaderStyle->Font = gcnew System::Drawing::Font( "Verdana",10,FontStyle::Bold );
          dataGridView1->ColumnHeadersDefaultCellStyle = columnHeaderStyle;
    
          // Set the column header names.
          dataGridView1->Columns[ 0 ]->Name = "Recipe";
          dataGridView1->Columns[ 1 ]->Name = "Category";
          dataGridView1->Columns[ 2 ]->Name = "Main Ingredients";
          dataGridView1->Columns[ 3 ]->Name = "Rating";
    
          // Populate the rows.
          array<String^>^row1 = gcnew array<String^>{
             "Meatloaf","Main Dish","ground beef","**"
          };
          array<String^>^row2 = gcnew array<String^>{
             "Key Lime Pie","Dessert","lime juice, evaporated milk","****"
          };
          array<String^>^row3 = gcnew array<String^>{
             "Orange-Salsa Pork Chops","Main Dish","pork chops, salsa, orange juice","****"
          };
          array<String^>^row4 = gcnew array<String^>{
             "Black Bean and Rice Salad","Salad","black beans, brown rice","****"
          };
          array<String^>^row5 = gcnew array<String^>{
             "Chocolate Cheesecake","Dessert","cream cheese","***"
          };
          array<String^>^row6 = gcnew array<String^>{
             "Black Bean Dip","Appetizer","black beans, sour cream","***"
          };
          array<Object^>^rows = {row1,row2,row3,row4,row5,row6};
          System::Collections::IEnumerator^ myEnum = rows->GetEnumerator();
          while ( myEnum->MoveNext() )
          {
             array<String^>^rowArray = safe_cast<array<String^>^>(myEnum->Current);
             dataGridView1->Rows->Add( rowArray );
          }
       }
    
       void Button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
       {
          // Resize the height of the column headers. 
          dataGridView1->AutoResizeColumnHeadersHeight();
    
          // Resize all the row heights to fit the contents of all non-header cells.
          dataGridView1->AutoResizeRows(
                DataGridViewAutoSizeRowsMode::AllCellsExceptHeaders);
       }
    
       void InitializeContextMenu()
       {
          // Create the menu item.
          MenuItem^ getRecipe = gcnew MenuItem( "Search for recipe",gcnew System::EventHandler( this, &Form1::OnMenuClick ) );
    
          // Add the menu item to the shortcut menu.
          System::Windows::Forms::ContextMenuStrip^ recipeMenu = gcnew System::Windows::Forms::ContextMenuStrip();
    
          // Set the shortcut menu for the first column.
          dataGridView1->Columns[ 0 ]->ContextMenuStrip = recipeMenu;
       }
    
       void OnMenuClick( Object^ /*sender*/, System::EventArgs^ /*e*/ )
       {
          if ( dataGridView1->CurrentCell != nullptr )
          {
             //Retrieve the recipe name.
             String^ recipeName = dynamic_cast<String^>(dataGridView1->CurrentCell->Value);
    
             //Search for the recipe.
             System::Diagnostics::Process::Start( String::Format( "http://search.msn.com/results.aspx?q={0}", recipeName ), nullptr );
          }
       }
    
    private:
    • Contrassegnato come rispostacheatcountry sabato 4 luglio 2009 3.32
    • Modificatojinzai giovedì 2 luglio 2009 20.53emphasize method
    •  
  • venerdì 3 luglio 2009 3.39Nishant SivakumarModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    If you are confused about the . vs -> difference, you really need to read up on C++/CLI before attempting to use it for WinForms development.
    http://blog.voidnish.com