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. C# How to get row value of DataGridView

C# How to get row value of DataGridView

Scheduled Pinned Locked Moved C#
databasequestioncsharptutorial
6 Posts 4 Posters 3 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.
  • L Offline
    L Offline
    LAPEC
    wrote on last edited by
    #1

    Hello Everyone Im generating two columns of dataGridView from database on click event button. The first column is FoodName, second column is FoodType. On my dataGridView I have set it a click event so the user can enter a number on particular cell of the third column like so...

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
    // make sure that the click was in the right column
    if (e.ColumnIndex == 2)
    {
    // Value is given in case the cell is empty
    string cellContent = "0";
    if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
    {
    cellContent = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
    }
    using (InputBox ib = new InputBox("Enter new stock amount:", this.dataGridView1[0, e.RowIndex].Value.ToString(), cellContent))
    {
    if (ib.ShowDialog() == DialogResult.OK)
    {
    this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = ib.Result;
    cellContent = ib.Result;
    this.dataGridView1.Controls.Find("btn" + cellValue, true); "THIS IS THE LINE WHERE I WANT TO GET THAT cellFoodTypeValue...

                    }
                }
            }
        } 
    

    How can I get the FoodType value from that row? Here is the code of my click event button... (Hence: ColFoodQtyStock and ColFoodStatus are template columns generated at run-time)

    private DataGridViewTextBoxColumn ColFoodQtyStock = new DataGridViewTextBoxColumn();
    private DataGridViewTextBoxColumn ColFoodStatus = new DataGridViewTextBoxColumn();

        private void cmdStarters\_Click(object sender, EventArgs e)
        {
            OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder();
            connBuilder.DataSource = @"C:\\Users\\AP\_AE\\Desktop\\DTPOS\_APP\\DataBase\\DtposMenu.accdb";
            connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
            connBuilder.Add("Jet OLEDB:Engine Type", "5");
    
            // Food SQL Query
            string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";
            using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
            {
                dataGridView1.Columns.Clear();
    
    D L L 5 Replies Last reply
    0
    • L LAPEC

      Hello Everyone Im generating two columns of dataGridView from database on click event button. The first column is FoodName, second column is FoodType. On my dataGridView I have set it a click event so the user can enter a number on particular cell of the third column like so...

      private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
      {
      // make sure that the click was in the right column
      if (e.ColumnIndex == 2)
      {
      // Value is given in case the cell is empty
      string cellContent = "0";
      if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
      {
      cellContent = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
      }
      using (InputBox ib = new InputBox("Enter new stock amount:", this.dataGridView1[0, e.RowIndex].Value.ToString(), cellContent))
      {
      if (ib.ShowDialog() == DialogResult.OK)
      {
      this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = ib.Result;
      cellContent = ib.Result;
      this.dataGridView1.Controls.Find("btn" + cellValue, true); "THIS IS THE LINE WHERE I WANT TO GET THAT cellFoodTypeValue...

                      }
                  }
              }
          } 
      

      How can I get the FoodType value from that row? Here is the code of my click event button... (Hence: ColFoodQtyStock and ColFoodStatus are template columns generated at run-time)

      private DataGridViewTextBoxColumn ColFoodQtyStock = new DataGridViewTextBoxColumn();
      private DataGridViewTextBoxColumn ColFoodStatus = new DataGridViewTextBoxColumn();

          private void cmdStarters\_Click(object sender, EventArgs e)
          {
              OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder();
              connBuilder.DataSource = @"C:\\Users\\AP\_AE\\Desktop\\DTPOS\_APP\\DataBase\\DtposMenu.accdb";
              connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
              connBuilder.Add("Jet OLEDB:Engine Type", "5");
      
              // Food SQL Query
              string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";
              using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
              {
                  dataGridView1.Columns.Clear();
      
      D Offline
      D Offline
      Dalek Dave
      wrote on last edited by
      #2

      Something like this? Obviously you change the column numbers to those required.

      1 Reply Last reply
      0
      • L LAPEC

        Hello Everyone Im generating two columns of dataGridView from database on click event button. The first column is FoodName, second column is FoodType. On my dataGridView I have set it a click event so the user can enter a number on particular cell of the third column like so...

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        // make sure that the click was in the right column
        if (e.ColumnIndex == 2)
        {
        // Value is given in case the cell is empty
        string cellContent = "0";
        if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
        {
        cellContent = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
        }
        using (InputBox ib = new InputBox("Enter new stock amount:", this.dataGridView1[0, e.RowIndex].Value.ToString(), cellContent))
        {
        if (ib.ShowDialog() == DialogResult.OK)
        {
        this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = ib.Result;
        cellContent = ib.Result;
        this.dataGridView1.Controls.Find("btn" + cellValue, true); "THIS IS THE LINE WHERE I WANT TO GET THAT cellFoodTypeValue...

                        }
                    }
                }
            } 
        

        How can I get the FoodType value from that row? Here is the code of my click event button... (Hence: ColFoodQtyStock and ColFoodStatus are template columns generated at run-time)

        private DataGridViewTextBoxColumn ColFoodQtyStock = new DataGridViewTextBoxColumn();
        private DataGridViewTextBoxColumn ColFoodStatus = new DataGridViewTextBoxColumn();

            private void cmdStarters\_Click(object sender, EventArgs e)
            {
                OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder();
                connBuilder.DataSource = @"C:\\Users\\AP\_AE\\Desktop\\DTPOS\_APP\\DataBase\\DtposMenu.accdb";
                connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
                connBuilder.Add("Jet OLEDB:Engine Type", "5");
        
                // Food SQL Query
                string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";
                using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
                {
                    dataGridView1.Columns.Clear();
        
        L Offline
        L Offline
        Luc 648011
        wrote on last edited by
        #3

        test

        1 Reply Last reply
        0
        • L LAPEC

          Hello Everyone Im generating two columns of dataGridView from database on click event button. The first column is FoodName, second column is FoodType. On my dataGridView I have set it a click event so the user can enter a number on particular cell of the third column like so...

          private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
          {
          // make sure that the click was in the right column
          if (e.ColumnIndex == 2)
          {
          // Value is given in case the cell is empty
          string cellContent = "0";
          if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
          {
          cellContent = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
          }
          using (InputBox ib = new InputBox("Enter new stock amount:", this.dataGridView1[0, e.RowIndex].Value.ToString(), cellContent))
          {
          if (ib.ShowDialog() == DialogResult.OK)
          {
          this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = ib.Result;
          cellContent = ib.Result;
          this.dataGridView1.Controls.Find("btn" + cellValue, true); "THIS IS THE LINE WHERE I WANT TO GET THAT cellFoodTypeValue...

                          }
                      }
                  }
              } 
          

          How can I get the FoodType value from that row? Here is the code of my click event button... (Hence: ColFoodQtyStock and ColFoodStatus are template columns generated at run-time)

          private DataGridViewTextBoxColumn ColFoodQtyStock = new DataGridViewTextBoxColumn();
          private DataGridViewTextBoxColumn ColFoodStatus = new DataGridViewTextBoxColumn();

              private void cmdStarters\_Click(object sender, EventArgs e)
              {
                  OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder();
                  connBuilder.DataSource = @"C:\\Users\\AP\_AE\\Desktop\\DTPOS\_APP\\DataBase\\DtposMenu.accdb";
                  connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
                  connBuilder.Add("Jet OLEDB:Engine Type", "5");
          
                  // Food SQL Query
                  string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";
                  using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
                  {
                      dataGridView1.Columns.Clear();
          
          L Offline
          L Offline
          Luc 648011
          wrote on last edited by
          #4

          Hi, something is wrong in the C# forum right now, so this message will not show as is, until things get fixed, and you won't be able to react either, you could however start a new thread if necessary. My reply is rather simple: inside the ContentClicked handler, you know the row and column index of the cell you clicked; and you can access the Rows, Columns, and Cells arrays using integer indices. So you probably want

          value=dataGridView1.Rows[e.RowIndex].Cells[the_index_of_the_column_you_want];

          You may need a .Value at the end. Hope this helps. :)

          1 Reply Last reply
          0
          • L LAPEC

            Hello Everyone Im generating two columns of dataGridView from database on click event button. The first column is FoodName, second column is FoodType. On my dataGridView I have set it a click event so the user can enter a number on particular cell of the third column like so...

            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
            // make sure that the click was in the right column
            if (e.ColumnIndex == 2)
            {
            // Value is given in case the cell is empty
            string cellContent = "0";
            if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
            {
            cellContent = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
            }
            using (InputBox ib = new InputBox("Enter new stock amount:", this.dataGridView1[0, e.RowIndex].Value.ToString(), cellContent))
            {
            if (ib.ShowDialog() == DialogResult.OK)
            {
            this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = ib.Result;
            cellContent = ib.Result;
            this.dataGridView1.Controls.Find("btn" + cellValue, true); "THIS IS THE LINE WHERE I WANT TO GET THAT cellFoodTypeValue...

                            }
                        }
                    }
                } 
            

            How can I get the FoodType value from that row? Here is the code of my click event button... (Hence: ColFoodQtyStock and ColFoodStatus are template columns generated at run-time)

            private DataGridViewTextBoxColumn ColFoodQtyStock = new DataGridViewTextBoxColumn();
            private DataGridViewTextBoxColumn ColFoodStatus = new DataGridViewTextBoxColumn();

                private void cmdStarters\_Click(object sender, EventArgs e)
                {
                    OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder();
                    connBuilder.DataSource = @"C:\\Users\\AP\_AE\\Desktop\\DTPOS\_APP\\DataBase\\DtposMenu.accdb";
                    connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
                    connBuilder.Add("Jet OLEDB:Engine Type", "5");
            
                    // Food SQL Query
                    string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";
                    using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
                    {
                        dataGridView1.Columns.Clear();
            
            D Offline
            D Offline
            Dalek Dave
            wrote on last edited by
            #5

            Please ignore the above reply, there has been a cockup in the c# forum.

            ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC League Table Link CCC Link[^]

            1 Reply Last reply
            0
            • L LAPEC

              Hello Everyone Im generating two columns of dataGridView from database on click event button. The first column is FoodName, second column is FoodType. On my dataGridView I have set it a click event so the user can enter a number on particular cell of the third column like so...

              private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
              {
              // make sure that the click was in the right column
              if (e.ColumnIndex == 2)
              {
              // Value is given in case the cell is empty
              string cellContent = "0";
              if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
              {
              cellContent = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
              }
              using (InputBox ib = new InputBox("Enter new stock amount:", this.dataGridView1[0, e.RowIndex].Value.ToString(), cellContent))
              {
              if (ib.ShowDialog() == DialogResult.OK)
              {
              this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = ib.Result;
              cellContent = ib.Result;
              this.dataGridView1.Controls.Find("btn" + cellValue, true); "THIS IS THE LINE WHERE I WANT TO GET THAT cellFoodTypeValue...

                              }
                          }
                      }
                  } 
              

              How can I get the FoodType value from that row? Here is the code of my click event button... (Hence: ColFoodQtyStock and ColFoodStatus are template columns generated at run-time)

              private DataGridViewTextBoxColumn ColFoodQtyStock = new DataGridViewTextBoxColumn();
              private DataGridViewTextBoxColumn ColFoodStatus = new DataGridViewTextBoxColumn();

                  private void cmdStarters\_Click(object sender, EventArgs e)
                  {
                      OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder();
                      connBuilder.DataSource = @"C:\\Users\\AP\_AE\\Desktop\\DTPOS\_APP\\DataBase\\DtposMenu.accdb";
                      connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
                      connBuilder.Add("Jet OLEDB:Engine Type", "5");
              
                      // Food SQL Query
                      string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";
                      using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
                      {
                          dataGridView1.Columns.Clear();
              
              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              OK, the forum is alive again, so here we go: IMO it is rather simple: inside the ContentClicked handler, you know the row and column index of the cell you clicked; and you can access the Rows, Columns, and Cells arrays using integer indices. So you probably want

              value=dataGridView1.Rows[e.RowIndex].Cells[the_index_of_the_column_you_want].Value;

              :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              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