如何使用MYSQl查询数据绑定GridView列 [英] How to Bind GridView Column with MYSQl Query Data

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

问题描述

我在asp.net中使用三列文本框进行gridview,需要在页面运行时加载select命令中的数据(在加载时)。在哪里写查询?



这里是gridview的设计代码..



i hv gridview in asp.net with three columns of textboxes, need to bind it with the data from select command at page runs(on load).where to write the query?

here is design code of gridview..

<asp:GridView ID="GridCall" runat="server" CssClass="Grid"

        AutoGenerateColumns="false" RowStyle-HorizontalAlign="Justify"

        ShowHeader="true" ShowFooter="false" AllowPaging="false"

        AlternatingRowStyle-BackColor="Cyan" GridLines="Horizontal" >

<HeaderStyle BackColor="AliceBlue" BorderStyle="Double" Height="25px" HorizontalAlign="Center" VerticalAlign="Middle"/>
<RowStyle BackColor="Wheat" BorderStyle="Solid" Height="20px" VerticalAlign="Middle" HorizontalAlign="Center"/>
<Columns>
<asp:TemplateField ShowHeader="true" HeaderText="Call ID" >
<ItemTemplate> <asp:TextBox ID="txtCallID" runat="server" CausesValidation="false" CssClass="txt"></asp:TextBox></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="true" HeaderText="P No" >
<ItemTemplate> <asp:TextBox ID="txtPNo" runat="server" CausesValidation="false" CssClass="txt"></asp:TextBox></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="true" HeaderText="Suspected Problems" >
<ItemTemplate> <asp:TextBox ID="txtProblem" runat="server" CausesValidation="false" CssClass="txt"></asp:TextBox></ItemTemplate>
</asp:TemplateField>
</Columns>





我的查询是......



and my query is ...

string strQry = "Select complain_date,complain_no,,pno, problem, pcno, status from call_log order by complain_date";
            GlobalDs = GlobalConectionClass.ExecuteDataSet(strQry);
            if (GlobalDs.Tables[0].Rows.Count > 0)
            {
                GridCall.DataSource = GlobalDs.Tables[0].DefaultView;
            }
            GlobalDs.Dispose();

推荐答案

我将向您展示如何使用sql绑定asp gridview两种方式。

方法1:你可以使用
I will show you how to bind asp gridview with sql by both the ways.
Method 1: You can use
<asp:BoundField>



方法2:你可以使用


Method 2: You can use

<asp:TemplateField> with <%# Bind(ColumnName)%>





这是我的演示代码 -

我有SQL结构如下



Here's My Demo Code-
I have SQL Structure as follows

Roll_Number	Student_Name	Address
101		Sammy		Mumbai
102		Rob		Delhi
103		Jacob		Banglore
104		Larry		Kolkatta
105		Hogg		Hyderabad





这是ASPX页面代码:



Here's ASPX page code:

<asp:GridView ID="gridDisplay" runat="server" DataSourceID="source_sql" AutoGenerateColumns="False">
	<Columns>
		<%-- This Field Is BOUND with Roll_Number column of Database --%>
		<asp:BoundField HeaderText="Seat Number" DataField="Roll_Number"/>

		<%-- This Field Is BOUND with Student_Name column of Database --%>
		<asp:BoundField HeaderText="Student Name" DataField="Student_Name" />

		<%-- This Field Is BOUND with Default column of Database--%>
		<asp:TemplateField HeaderText="Student Address" ItemStyle-Width="10%">
			<ItemTemplate>
				<asp:Label ID="lblAddress" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
			</ItemTemplate>
		</asp:TemplateField>
	</Columns>
</asp:GridView>

<asp:SqlDataSource ID="source_sql" runat="server" ConnectionString="Your Connection String Here" SelectCommand="select * from Students">
</asp:SqlDataSource>


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

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