1
votes

Im trying to create a program that select row 1 at a time and print specific column in messagebox but the output was only the first item and will be repeated based on how many rows in my datagridview. Anyone knows what im missing? Thanks in advance

dgvCart was my datagridview

private void button1_Click_2(object sender, EventArgs e)

    {
        for (int i = 0 ; i < dgvCart.Rows.Count; i++)
        {
            dgvCart.ClearSelection();
            dgvCart.Rows[i].Selected = true;

            if (dgvCart.SelectedCells.Count > 0)
            {
                int selectedrowindex = dgvCart.SelectedCells[i].RowIndex;

                DataGridViewRow selectedRow = dgvCart.Rows[selectedrowindex];

              string  _name = Convert.ToString(selectedRow.Cells["Column1"].Value);
              decimal _quantity = Convert.ToDecimal(selectedRow.Cells["Column3"].Value);
              MessageBox.Show("NAME: " +   "Quantity: " + _quantity.ToString());
                dgvCart.Rows[i].Selected = false;
            }
          //  string _name = dgvCart.CurrentRow.Cells[0].Value.ToString();
          //  decimal _quantity = Convert.ToDecimal(dgvCart.CurrentRow.Cells[2].Value.ToString());


        }

    }
2
Not very clear what your problem is. Output is always same row? Or what?Liquid Core
sorry my english is not that good. first the system need to select every row 1 at a time , after selecting a single row the system need to print the value of the column1 and column 3 using messagebox. then select the next row and repeat the process until the entire rows on the datatable print their column what happens to my code was it only print the first selected rows but reapeat the displaying of messagebox based on row count(which is right) . P.S. Sorry, I dont know how to do the formatting in commentMekan FLores
Ok, now I got itLiquid Core

2 Answers

3
votes

IF YOU WANT TO SHOW ONLY SELECTED ROWS OF GRID THEN TRY THIS

foreach(DataGridViewRow Rows in this.dataGridViewX1.Rows)

        {
            if (Rows.Selected)
            {
                MessageBox.Show("NAME: " +Rows.Cells[0].Value +  "Quantity: " + Rows.Cells[1].Value);

            }
        }

IF YOU WANT TO SHOW ALL ROWS DATA

THEN USE THE CODE

foreach (DataGridViewRow rows in this.dataGridViewX1.Rows)

        {
            if (rows.IsNewRow) { }
            else {
                MessageBox.Show(rows.Cells[0].Value.ToString() + "','" + rows.Cells[1].Value.ToString()) ;
                }
        }
0
votes

I just copy pasted your code and it worked fine as per your desired output. Please have a look at below code:

    private void Form1_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("id");
        dt.Columns.Add("name");
        dt.Columns.Add("quantity");

        DataRow dr;

        dr = dt.NewRow();
        dr["id"] = "1";
        dr["name"] = "mobile";
        dr["quantity"] = "100";

        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["id"] = "2";
        dr["name"] = "laptop";
        dr["quantity"] = "50";

        dt.Rows.Add(dr);

        this.dataGridView1.DataSource = dt;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            dataGridView1.ClearSelection();
            dataGridView1.Rows[i].Selected = true;

            if (dataGridView1.SelectedCells.Count > 0)
            {
                int selectedrowindex = dataGridView1.SelectedCells[i].RowIndex;

                DataGridViewRow selectedRow = dataGridView1.Rows[selectedrowindex];

                string _name = Convert.ToString(selectedRow.Cells["name"].Value);
                decimal _quantity = Convert.ToDecimal(selectedRow.Cells["quantity"].Value);
                MessageBox.Show("name: " + "quantity: " + _quantity.ToString());
                dataGridView1.Rows[i].Selected = false;
            }
        }
    }