如何使用SqlDataReader绑定DataGridView [英] How to Bind DataGridView with SqlDataReader

查看:40
本文介绍了如何使用SqlDataReader绑定DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以在此代码中为我提供帮助.我正在尝试用datagridview绑定数据.查询正在返回SQL Server中的值.但这并没有绑定DataGridView中的任何值....

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();
        }
   }

推荐答案

为列"设计器中的每个列设置 DataPropertyName .为此,您可以将鼠标指针放在 DataPropertyName 行的右侧单元格上,并在其上显示(无),然后单击鼠标左键.选择显示为(无)"的文本,然后在该 DataGridView 列中键入要显示的数据库字段名称.

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.

这篇关于如何使用SqlDataReader绑定DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆