我要显示的数据是数据库到网格视图控制 [英] i want to display data that is in database into grid view control

查看:118
本文介绍了我要显示的数据是数据库到网格视图控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检索存储在数据库中插入GridView控件的表中的数据和我的ADO code是

 公共无效retrieve_client()
    {
        SqlConnection的CON =新的SqlConnection(DBconnection.connectstr);
        con.Open();
        COM的SqlCommand =新的SqlCommand(retrieve_clientCON);
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add(@名,SqlDbType.VarChar).value的= this.name;
        SqlDataReader的R = com.ExecuteReader();
        如果(r.HasRows)
        {
            如果(r.Read())
            {
                this.name = R [0]的ToString();
                this.address = R [1]的ToString();
                this.phone = R [1]的ToString();
            }
        }
        r.Close();
        con.Close();
    }

和我的web表单来源是我想输入文本框中的名称和检索数据并显示他们到网格视图控件,请回答我:

我的Web表单来源是:

 <表>
&所述; TR>
    &所述; TD>        <标签类=labelclient>名称和LT; /标签>
            < / TD>    < TD类=clientpadding>        < ASP:文本框ID =Textbox_retrieveclientbyname_first=服务器占位符=名的CssClass =textboxstyle所需=需要>< / ASP:文本框>        &安培; NBSP; &安培; NBSP;
            < ASP:文本框ID =Textbox_retrieveclientbyname_second=服务器占位符=第二个名字的CssClass =textboxstyle所需=需要>< / ASP:文本框>         &安培; NBSP; &安培; NBSP;    < ASP:文本框ID =Textbox_retrieveclientbyname_third=服务器占位符=第三名的CssClass =textboxstyle所需=需要>< / ASP:文本框>      < / TD>< / TR>
< /表>
< BR />
< BR />
<表>
    &所述; TR>
        < TD类=buttontd>
            < ASP:按钮的ID =btn_find_clientbyname=服务器文本=查找的CssClass =addclientbutton的OnClick =btn_find_clientbyname_Click/>
        < / TD>
        &所述; TD>
            < ASP:标签ID =lbl_ermsg=服务器>< / ASP:标签>
        < / TD>
    < / TR>
< /表>
< D​​IV>
    < ASP:GridView控件ID =GridView_clientbyname=服务器>
        < HeaderStyle的CssClass =gridheader/>
        < RowStyle的CssClass =gridrow/>
        < AlternatingRowStyle的CssClass =gridaltrow/>
    < / ASP:GridView的>
< / DIV>

`


解决方案

  

使用的DataAdapter 来填充数据表和绑定的 GridView控件随着
  这数据表


 公共无效retrieve_client()
    {
        SqlConnection的CON =新的SqlConnection(DBconnection.connectstr);
        con.Open();
        COM的SqlCommand =新的SqlCommand(retrieve_clientCON);
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add(@名,SqlDbType.VarChar).value的= this.name;
        SqlDataAdapter的大=新SqlDataAdapter的(COM);
        DataTable的DT =新DatTable();
        da.Fill(DT);
        con.Close();
        GridView_clientbyname.DataSource = DT;
        GridView_clientbyname.DataBind();    }

i want to retrieve the data stored in the table of the database into gridview control and my ado code is

   public void retrieve_client()
    {
        SqlConnection con = new SqlConnection(DBconnection.connectstr);
        con.Open();
        SqlCommand com = new SqlCommand("retrieve_client", con);
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@name", SqlDbType.VarChar).Value = this.name;
        SqlDataReader r = com.ExecuteReader();
        if(r.HasRows)
        {
            if(r.Read())
            {
                this.name =    r[0].ToString();
                this.address = r[1].ToString();
                this.phone =   r[1].ToString();
            }
        }
        r.Close();
        con.Close();
    }

and my web form source is i want to enter the name in text box and retrieve the data and displaying them into grid view control please answer me:

my web form source is:

    <table>
<tr>
    <td>

        <label class="labelclient">Name</label>
            </td>

    <td class="clientpadding">

        <asp:TextBox ID="Textbox_retrieveclientbyname_first" runat="server" placeholder=" First Name" cssclass="textboxstyle" required="required"></asp:TextBox>

        &nbsp; &nbsp; 


            <asp:TextBox ID="Textbox_retrieveclientbyname_second" runat="server" placeholder=" Second Name" CssClass="textboxstyle" required="required"></asp:TextBox>

         &nbsp; &nbsp; 

    <asp:TextBox ID="Textbox_retrieveclientbyname_third" runat="server" placeholder=" Third Name" CssClass="textboxstyle" required="required"></asp:TextBox>

      </td>

</tr>
</table>
<br />
<br />
<table>
    <tr>
        <td class="buttontd">
            <asp:Button ID="btn_find_clientbyname" runat="server" Text="Find" CssClass="addclientbutton" OnClick="btn_find_clientbyname_Click"/>
        </td>
        <td>
            <asp:Label ID="lbl_ermsg" runat="server" ></asp:Label>
        </td>
    </tr>
</table>
<div>
    <asp:GridView ID="GridView_clientbyname" runat="server" >
        <HeaderStyle CssClass="gridheader"/>
        <RowStyle CssClass="gridrow" />
        <AlternatingRowStyle cssclass="gridaltrow" />
    </asp:GridView>
</div>

`

解决方案

Use DataAdapter to Fill DataTable And bind the GridView With That DataTable

 public void retrieve_client()
    {
        SqlConnection con = new SqlConnection(DBconnection.connectstr);
        con.Open();
        SqlCommand com = new SqlCommand("retrieve_client", con);
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@name", SqlDbType.VarChar).Value = this.name;
        SqlDataAdapter da = New SqlDataAdapter(com);
        DataTable dt=New DatTable();
        da.Fill(dt);
        con.Close();
        GridView_clientbyname.DataSource=dt;
        GridView_clientbyname.DataBind();

    }

这篇关于我要显示的数据是数据库到网格视图控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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