locked
Read every row in a Gridview and display it on the label RRS feed

  • Question

  • Hi there,

    I would like to show the seleceted row fields to be displayed on the label. I tried to use the for loop, but only the first data from the database is being displayed.

    Here are my codes.

      

    Thanks people! ;)

    string

     

    strConnectionString = ConfigurationManager.ConnectionStrings["EMSConnectionString"

    ].ConnectionString;

     

    SqlConnection myConnect = new SqlConnection

    (strConnectionString);

     

    string strCommandText = "SELECT [Order Details].OrderQty, Product.Qty FROM [Order Details] INNER JOIN Product ON [Order Details].ProdID = Product.ProdID"

    ;

    myConnect.Open();

     

     

    SqlCommand cmd = new SqlCommand

    (strCommandText, myConnect);

     

    SqlDataReader

    reader = cmd.ExecuteReader();

     

    if

    (reader.Read())

    {

     

    for (int

    i =0; i < GridView1.Rows.Count; i++)

    {

    Label3.Text = reader[

    "OrderQty"

    ].ToString();

    Label4.Text = reader[

    "Qty"

    ].ToString();

    }

    orderqty = Label3.Text;

    prodqty = Label4.Text;

    pq =

    Convert

    .ToInt32(prodqty);

    oq =

    Convert

    .ToInt32(orderqty);

     

     

     

    myConnect.Close();

    }

    }

     

     

    • Moved by Cookie Luo Sunday, July 10, 2011 12:57 PM (From:Visual C# General)
    Thursday, June 30, 2011 6:31 AM

Answers

  • Hi

    That's because Erici is using DataGridView not GridView. So if it will not work in asp.net.

    I have done the following example.

     DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
          dt.Columns.Add(new DataColumn("1"));
          dt.Columns.Add(new DataColumn("2"));
          dt.Columns.Add(new DataColumn("3"));
          DataRow dr = dt.NewRow();
          dr["1"] = "1";
          dr["2"] = "2";
          dr["3"] = "3";
          dt.Rows.Add(dr);
          this.GridView1.DataSource = dt;
          this.GridView1.DataBind();
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
          this.TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;
          this.TextBox2.Text = GridView1.SelectedRow.Cells[2].Text;
        }
    

    When you select the current row, the output in the textboxs are "1" and "2". For further problems about asp.net, please try asp.net forum.

    Hope it helps.


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Schizo69 Wednesday, December 14, 2011 7:27 AM
    Tuesday, July 5, 2011 3:56 AM

All replies

  • Hi,

    just change

     

    if
    
    (reader.Read())
    
    {
    

     

    with

     

    while(reader.Read())
    {
    
    }
    


    Regards,

    Eric


    Failure is not the worst thing in the world. The very worst is not to try. Email Address : ericjohnadamos@gmail.com. http://ericjohnadamos.blogspot.com/
    Thursday, June 30, 2011 6:59 AM
  • It doesn't solve the problem. When using while(reader.Read()), it display the last data in my database.

    Thursday, June 30, 2011 11:12 AM
  • where are these function implemented? in a button click?
    or in a datagridview_cellclick?

    If you are putting this in a button, and display in a single text, of course it will display the last item in your database.

    But if you are referring to a cell click, just do

    using(DataGridViewRow dgvr in this.dataGrid1.Rows)
    {
    
    	if(dgvr.Selected)
    
    	{
    
    		this.Label3.Text = dgvr.Cells["OrderQty"].Value.ToString();
    
    		this.Label4.Text = dgvr.Cells["Qty"].Value.ToString();
    
    		break;
    
    	}
    
    } 
    


     


    Failure is not the worst thing in the world. The very worst is not to try. Email Address : ericjohnadamos@gmail.com. http://ericjohnadamos.blogspot.com/
    Friday, July 1, 2011 4:27 AM
  • I'm putting this in the  GridView1_SelectedIndexChanged

    I don't understand your codes plus there's error when i paste your codes on my project.
    Friday, July 1, 2011 5:40 AM
  • Hi

    That's because Erici is using DataGridView not GridView. So if it will not work in asp.net.

    I have done the following example.

     DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
          dt.Columns.Add(new DataColumn("1"));
          dt.Columns.Add(new DataColumn("2"));
          dt.Columns.Add(new DataColumn("3"));
          DataRow dr = dt.NewRow();
          dr["1"] = "1";
          dr["2"] = "2";
          dr["3"] = "3";
          dt.Rows.Add(dr);
          this.GridView1.DataSource = dt;
          this.GridView1.DataBind();
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
          this.TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;
          this.TextBox2.Text = GridView1.SelectedRow.Cells[2].Text;
        }
    

    When you select the current row, the output in the textboxs are "1" and "2". For further problems about asp.net, please try asp.net forum.

    Hope it helps.


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Schizo69 Wednesday, December 14, 2011 7:27 AM
    Tuesday, July 5, 2011 3:56 AM