本文介绍了如何以编程方式从单个数据库列填充网格视图的两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在为客户及其介绍人创建一个gridview。我创建了一个网格视图并用数据库值填充它。但是我需要在网格视图中使用介绍人名称以及他们没有。但我只有一个单独的intoducer id列。我必须通过他们的会员ID获得介绍人的名字。我不知道如何填充该列。请帮助我。 我的代码是 Member_status.aspx < asp:GridView ID = bussgrid runat = server AutoGenerateColumns = False BorderColor = Aqua 宽度 = 520px 高度 = 143px > < 列 > < asp:BoundField DataField = Member_Name HeaderText = 名称 HeaderStyle-BorderColor = Aqua HeaderStyle-Font-Names = verdana HeaderStyle-Font-Size = 7 HeaderStyle-ForeColor = 紫色 SortExpression = Member_Name / > < asp:BoundField DataField = CombinedId HeaderText = MemberID ReadOnly = True HeaderStyle-BorderColor = Aqua HeaderStyle-Font-Names = verdana HeaderStyle-Font-Size = 7 HeaderStyle-ForeColor = 紫色 SortExpression = CombinedId / > < asp:BoundField DataField = IntroducerID HeaderText = IntroducerID HeaderStyle-BorderColor = Aqua HeaderStyle-Font-Names = verdana HeaderStyle-Font-Size = 7 HeaderStyle-ForeColor = 紫色 SortExpression = IntoducerID / > < asp:BoundField DataField = Placement_Side HeaderText = 位置 HeaderStyle -BorderColor = Aqua HeaderStyle-Font-Names = verdana HeaderStyle-Font-Size = 7 HeaderStyle-ForeColor = 紫色 排序表达式 = Placement_Side / > < asp:TemplateField HeaderText = 介绍人姓名 > < ItemTemplate > < asp:标签 ID = Intro_Name runat = 服务器 > < / asp:标签 > <% - < asp:Literal ID = Intro_Name runat = server /> - %> < / ItemTemplate > < / asp:TemplateField > < /列 > < / asp:GridView > member_status.aspx.cs protected void Button1_Click(object sender,EventArgs e) { string text =select Member_Name,CombinedId,Gender,Member_DOB,[Placement_Side],[City],[Join_Date],IntroducerID,SponsorID from Member_registration where IntroducerID ='+ TextBox1.Text +'; string text1 = text +从Member_Registration中选择Member_Name,其中CombinedID ='+ TextBox1.Text +'; SqlCommand cmd = new SqlCommand(text1,connection); connection.Open(); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); bussgrid.DataSource = dt; bussgrid.DataBind(); } protected void bussgrid_RowDataBound(object sender,GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.DataRow) { } 解决方案 你可以参考下面的链接更多的想法 display-rows-in-multiple-columns-in-asp-net-gridview [ ^ ] 你可以使用这个查询 选择IntoducerID +''+ Member_Name作为no_name,来自abc_table的Placement_Side 并绑定你的gridview后 您必须绑定要在gridview中显示的数据。您可以绑定一个字段在网格视图的一列中。因此在选择查询中,您必须选择两次相同的字段。 Hi, I am creating a gridview for customer and their introducer.I have created a grid view and populated it with the database values.But i need the introducer name in my grid view along with their no.But i have a seperate column for intoducer id only.I have to get the name of introducer by their member ID.I don'T know how to populate that column .Kindly help me.And my code isMember_status.aspx<asp:GridView ID="bussgrid" runat="server" AutoGenerateColumns="False" BorderColor="Aqua" Width="520px" Height="143px" > <Columns> <asp:BoundField DataField="Member_Name" HeaderText="Name" HeaderStyle-BorderColor="Aqua" HeaderStyle-Font-Names="verdana" HeaderStyle-Font-Size="7" HeaderStyle-ForeColor="Purple" SortExpression="Member_Name" /> <asp:BoundField DataField="CombinedId" HeaderText="MemberID" ReadOnly="True" HeaderStyle-BorderColor="Aqua" HeaderStyle-Font-Names="verdana" HeaderStyle-Font-Size="7" HeaderStyle-ForeColor="Purple" SortExpression="CombinedId" /> <asp:BoundField DataField="IntroducerID" HeaderText="IntroducerID" HeaderStyle-BorderColor="Aqua" HeaderStyle-Font-Names="verdana" HeaderStyle-Font-Size="7" HeaderStyle-ForeColor="Purple" SortExpression="IntoducerID" /> <asp:BoundField DataField="Placement_Side" HeaderText="Position" HeaderStyle-BorderColor="Aqua" HeaderStyle-Font-Names="verdana" HeaderStyle-Font-Size="7" HeaderStyle-ForeColor="Purple" SortExpression="Placement_Side" /> <asp:TemplateField HeaderText="Introducer Name"> <ItemTemplate> <asp:Label ID="Intro_Name" runat="server"></asp:Label> <%--<asp:Literal ID="Intro_Name" runat="server" />--%> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>member_status.aspx.csprotected void Button1_Click(object sender, EventArgs e) { string text = "select Member_Name,CombinedId,Gender,Member_DOB,[Placement_Side],[City],[Join_Date],IntroducerID,SponsorID from Member_registration where IntroducerID='" + TextBox1.Text + "' "; string text1 = text + " select Member_Name from Member_Registration where CombinedID='"+ TextBox1.Text +"'"; SqlCommand cmd = new SqlCommand(text1, connection); connection.Open(); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); bussgrid.DataSource = dt; bussgrid.DataBind(); } protected void bussgrid_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { } 解决方案 you can refer below link for more idea on thisdisplay-rows-in-multiple-columns-in-asp-net-gridview[^]yout can use this queryselect IntoducerID+' '+Member_Name as no_name,Placement_Side from abc_tableand after bind your gridviewyou have to bind the data to show in the gridview.you can bind one field to one column of the grid view.so in the select query you have to select the same field twice. 这篇关于如何以编程方式从单个数据库列填充网格视图的两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 13:38