4

Can anyone help me in this Code Please. I am trying to bind data with datagridview. Query is returning values in sql server. But it's not binding any value in DataGridView....

     private void CheckMembers()
     {
        try
        {
            string query = "Select id, id-no, status From Members Where Head=@Head";

            sqlCommand = new SqlCommand(query, sqlConnection);
            sqlConnection.Open();
            sqlCommand.Parameters.AddWithValue("@Head", "2288885858");
            sqlDataReader = sqlCommand.ExecuteReader();

            if (sqlDataReader.HasRows)
            {
                fmGview.Visible = true;
                DataTable dt = new DataTable();
                dt.Load(sqlDataReader);
                MessageBox.Show(dt.ToString());
                fmGview.DataSource = dt;
            }
        }
        catch (Exception exp)
        {
            MessageBox.Show(exp.ToString(), "Exception in CheckMembers");
        }
        finally
        {
            CheckConnectionStatus();
        }
   }

enter image description here

16
  • 3
    This should work i think. Are you sure your sql find some data? Commented Nov 7, 2016 at 12:18
  • 1
    i tested your code. it works fine. maybe your fieldname on grid has a typo or something. Commented Nov 7, 2016 at 12:24
  • 1
    if you double-checked and dt is not empty, please also post yout UI code. DataGridView doesn't belong to wpf Commented Nov 7, 2016 at 13:41
  • 1
    @MistrMowgli, i don't know. you didn't show any UI code Commented Nov 7, 2016 at 14:18
  • 1
    @EdPlunkett, 3 in the counter of rows in dt Commented Nov 7, 2016 at 14:58

1 Answer 1

5

Set DataPropertyName for each column in the Columns designer. You do this by putting the mouse pointer over the right hand cell in the DataPropertyName row, where it says (none), and clicking the left mouse button. Select the text that says "(none)", and type the database field name that you want to be displayed in that DataGridView column.

Sign up to request clarification or add additional context in comments.

7 Comments

Click the mouse in the box and type the name of the field. In the one that you want to be id, type "id".
It start inserting blank rows only
@MistrMowgli What did you type in that box?
Thanks for helping... Really Appreciated
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.