How the datagridview row count on cell end Edit

Locked How the datagridview row count on cell end Edit

  • Thursday, April 05, 2012 3:34 PM
     
      Has Code

    hi my problem is when i enter datagridview cell in 'ITEM NAME' the 'PRICE' will appear in 'PRICE' Column..but i didnt how to count the datagridview..

    My table name is PRODUCT.. (eg) two items like

    APPLE  - rs. 200

    ORANGE- rs. 100

    i enter ITEM NAME in datagridview PRICE  cell will change '0' column only...

    this is happen...

    My code is here...pls any one modify the code..

            private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                con.Open();
                cmd.Connection = con;
                cmd.CommandText = "Select PRICE from PRODUCT where Item='"+dataGridView1.CurrentRow.Cells["Column1"].Value.ToString()+"'";
                dr = cmd.ExecuteReader();
                int rr = 0;
                while (dr.Read())
                {
                    dataGridView1.Rows[rr].Cells["Column3"].Value = dr.IsDBNull(0) ? "" : dr.GetInt32(0).ToString();
                }
                con.Close();
            }
    pls i need it.. thnk you..

    Mankatha



    • Edited by Mankatha da Thursday, April 05, 2012 3:53 PM
    •  

All Replies

  • Thursday, April 05, 2012 4:22 PM
     
     

    Some strange code you have there.

    Sorry, but I really didnt understand what you would like to do. Can you try with anoher, better, explanation?

    Why you use CellEndEdit event? You should specify some limitations, like when should this code inside this event occur. I bet not in all columns, but only on some of them (or just in one column).

    Answer me, On which column CellEndEdit event would you like to rise?


    Mitja


  • Thursday, April 05, 2012 4:44 PM
     
     

    when i edit datagridview cell 'ITEMNAME'  the 'PRICE' cell get the price value from database..

    tat only i say PRODUCT table have two items(eg)

    Apple is 200

    orange is 100

    first i enter 'apple' in 'ITEMNAME' column the PRICE shown 200...(1st Image above)

    second 'ITEMNAME' enter 'orange' the price shown same cell... (2nd Image above)

    like above shown image..

    i need Apple value is 200 in one row...

    next row orange is 100...and next,next,go like that...

    how can i do it....


    Mankatha


    • Edited by Mankatha da Thursday, April 05, 2012 4:51 PM
    •  
  • Thursday, April 05, 2012 4:47 PM
     
     
    So, you mean, when you type "Apple" or "Orange" and after pressing enter key, to leave the cell, the Price column should update wit the appropriate price accordingly. Am I right?

    Mitja

  • Thursday, April 05, 2012 4:52 PM
     
      Has Code

    I think , you doing the select clause in the wrong event, because, if u need update value on database when edit some cell, you can't make the select in the same event(I believe). Usually i make that in a diferrent way, for example: lets suppose that i have one datagridview with 2 columns(name,age) and 2 textbox(name,age), when i click in some row on my DGV, the values automatically appears in respectives textbox, and finally i click on update button to do what he need to do on database.

    You can do that using the SelectionChanged like this:

            private void dataGridView1_SelectionChanged(object sender, EventArgs e)
            {
                if (dataGridView1.SelectedRows.Count == 1)
                {
                    yourTextBox.Text = dataGridView1.SelectedRows[0].Cells["yourColumnName"].Value.ToString();
                }
            }
    Hope this helps.

  • Thursday, April 05, 2012 4:53 PM
     
     
    yes mitja....

    Mankatha


    • Edited by Mankatha da Thursday, April 05, 2012 4:56 PM
    •  
  • Thursday, April 05, 2012 4:55 PM
     
     Answered Has Code

    If so you can do:

            private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == 0) //setting some restrictions when the code should occure - in your case only when leaving 1st column!
                {
                    con.Open();
                    cmd.Connection = con;
                    cmd.CommandText = "Select PRICE from PRODUCT where Item = @param";
                    cmd.Parameters.Add("@param", SqlDbType.Int).Value = dataGridView1["Column1", e.RowIndex].Value.ToString();
                    dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        dataGridView1["Column3", e.RowIndex].Value =  dr.IsDBNull(0) ? "" : dr.GetInt32(0).ToString();
                    }
                    else
                         MessageBox.Show("There is no price for this item in database.");
                    con.Close();
                }
            }

    If this is not it, please provide some more info.


    Mitja

    • Marked As Answer by Mankatha da Thursday, April 05, 2012 5:04 PM
    •  
  • Thursday, April 05, 2012 5:04 PM
     
     
    thnk you thnk you...its very helpful for me....thnk you.....

    Mankatha

  • Thursday, April 05, 2012 5:06 PM
     
     

    hehe, anytime mate. I am glad it works.

    Can you see (understand) why I used  if(condition) in the code?

    Answer: to limit your code which is inside, to only be executed when you are exiting 1st column and no where else.

    bye


    Mitja