0

My problem is that it will only display the contents of datagrid on textboxes when i click on the cells under Price column which has a Money DataType all others except for ItemNo is Varchar(100). Please help, thanks

     private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
            DataGridViewRow row = this.dataGridView1.CurrentRow;
            txtDsc.Text = row.Cells["Description"].Value.ToString();
            txtQty.Text = row.Cells["Qty"].Value.ToString();
            txtUnt.Text = row.Cells["Unit"].Value.ToString();
            txtPrc.Text = row.Cells["Price"].Value.ToString();
            txtRmr.Text = row.Cells["Remarks"].Value.ToString();
    }
  • Why you post your code as image? – Tim Schmelter Sep 20 '18 at 14:35
  • newbie here dont know how to sorry – Diether Noche Sep 20 '18 at 14:36
  • 1
    Ok, welcome to Stackoverflow :) Here is something you might want to read then: https://stackoverflow.com/help/how-to-ask and here's a checklist of the most important things to remember when asking questions: https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/ – Tim Schmelter Sep 20 '18 at 14:37
  • Please refrain from posting your code as an image as that makes it impossible to copy and paste as well as difficult for search engines to index. When asking questions it is best to include your code, expected output, current errors, as well as any steps that you may have tried. This will make it easier for the community to help you. – DCCoder Sep 20 '18 at 14:41
  • Noted. i already edited my question :) thanks – Diether Noche Sep 20 '18 at 14:43
  • I'm not sure I understand your question, can you try to elaborate and clarify a bit more what you're trying to accomplish? – JD Davis Sep 20 '18 at 14:53

2 Answers2

0

I'm not entirely sure why it is only populating when you click that cell; I recreated your code and it works just fine. I did manage to recreate it again using the following code:

    private void myDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        int row = myDGV.CurrentCell.RowIndex;
        txtDsc.Text = myDGV.Row[row].Cells["Description"].Value.ToString();
        txtQty.Text = myDGV.Row[row].Cells["Qty"].Value.ToString();
        txtUnt.Text = myDGV.Row[row].Cells["Unit"].Value.ToString();
        txtPrc.Text = myDGV.Row[row].Cells["Price"].Value.ToString();
        txtRmr.Text = myDGV.Row[row].Cells["Remarks"].Value.ToString();
    }
k794
  • 21
  • 7
0

As the other answer pointed out the code seems to be working fine. I suspect the event is not firing off. Try putting in a breakpoint and investigate that bit.

I am presuming you need CellClick instead of CellContentClick. Refer CellContentClick event doesn't always work

Alternatively if you are trying to display the text box values based on the selection on the data grid view, use DataGridView.SelectionChanged Event

Sri
  • 135
  • 1
  • 6