将数据绑定到模板字段 [英] Binding data to template fields

查看:68
本文介绍了将数据绑定到模板字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用后面代码中的数据源创建gridview。我能够填充一个基本的gridview,它只显示绑定列中的信息,但我需要将列作为模板字段。我试图把确切数量的模板字段放在有列但没有运气的情况下。使用我的sql语句和后面的代码中的所有内容,如何填充模板字段?



预先感谢

Sean8084



I'm trying to create a gridview using a datasource from the code behind. I'm able to populate a basic gridview that just displays the information in bound columns, but I need the columns to be template fields. I've tried to put the exact number of template fields as there are columns but with no luck. With my sql statement and everything in code behind, how do I make it populate the Template fields?

Thanks in Advance
Sean8084

protected void Page_Load (object sender, EventArgs e)
{
     if (total_Gridview.dataSource == null)
     {
          con.open();
          sql = (select statement);
          cmd = new SqlCommand(sql, con);
          total_Gridview.DataSource = cmd.ExecuteReader();
          total_Gridview.DataBind();
          con.close();
     }
}







<asp:GridView ID="total_Gridview" runat="server" AutoGenerateColumns="False">
     <columns>
          <asp:TemplateField>
               <HeaderTemplate>
               </HeaderTemplate>
          
     </columns>



我的c#代码说sql语句,它是一个select语句,只是不能透露它的内容。

这是它的主旨,如果你需要了,请告诉我。



提前致谢

Sean8084


where my c# code says sql statement, it's a select statement, just can't disclose what it says.
that's the jist of it, if you need anymore, just let me know.

Thanks in advance
Sean8084

推荐答案

根据您的SELECT语句,您必须创建匹配的字段。如果在select语句中选择4个字段,则必须在gridview中具有匹配的字段才能显示它们。因此,在绑定gridview之前,必须在绑定之前动态创建字段。



要动态创建绑定列,请使用以下代码;



Depending on your "SELECT" statement you have to create the matching fields. If you select 4 fields in your select statement, you have to have matching fields in your gridview to show them. So before binding the gridview, you have to create the fields dynamically before binding.

For dynamically creating bound column you use the following code;

BoundField nameColumn = new BoundField();
nameColumn.DataField = "Name";
nameColumn.HeaderText = "Person Name"; 
GridView1.Columns.Add(nameColumn);





要动态创建模板列,请使用以下代码;





For dynamically creating template column you use the following code;

TemplateField ckhColumn = new TemplateField();
ckhColumn.HeaderTemplate = new GridViewTemplate(ListItemType.Header, "CheckBox Column");
ckhColumn.ItemTemplate = new GridViewTemplate(ListItemType.Item, "some data");
GridView1.Columns.Add(ckhColumn);





另请看CP中的这篇文章



如何在网格视图中动态创建模板列 [ ^ ]

<祝你好运。祝你好运。



Also have a look at this article in CP

How to create template columns dynamically in a grid view[^]

Good luck.


让我解决你的问题。

例如你想要显示sql获取的gried视图中的列查询,您的模板字段在griedview中将如下所示
Let me solve your problem.
for example you want to show for columns in the gried view fetched by sql query, your template fields will look like this in the griedview
<Columns>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>





或者您可以在gridview标签之间复制并粘贴此代码,即



or you can copy and paste this code in between you gridview tag i.e.

<asp:GridView ID="GridView1" runat="server">

</asp:GridView>





它将创建带有四个模板字段的GridView四个你。



现在例如你的sql语句

就像从表名中选择名称,地址,国家,资格



然后将以下代码添加到gridview代码中(只需在所有TextBox和Label字段中添加Text属性)





It will create GridView with four Template Field four you.

Now for example your sql statement
is like " select Name, Address, Country, Qualification from tablename"

then add the below code to your gridview code(Just add Text property in all TextBox and Label field)

<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>











and same in the

<asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>





因此您的一列确切代码就像





So your exact code for your one column will be like

<asp:TemplateField HeaderText="User Id">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>





在Bind(请给出你的列名称确认)



同样为所有列添加此代码。



如果已经解决,请将其标记为已解决。



如果您有任何疑问,请回复我。



谢谢

Suman Zalodiya



In Bind("Pleae give Exact your Column Name")

Same add this code for all the column.

Please Mark it as Resolved if it resolved.

Please revert back to me in case of you find any doubt.

Thanks
Suman Zalodiya


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

                   ShowFooter="True" Height="137px"

                   Width="332px">
                   <Columns>
                    <asp:TemplateField HeaderText="sno">
                           <ItemTemplate>
                       <asp:Label ID="txtsno" runat="server" Text='<%# bind("sno") %>'></asp:Label>
                           </ItemTemplate>
                       </asp:TemplateField>

                       <asp:TemplateField HeaderText="Name of property">
                           <ItemTemplate>
                               <asp:Label ID="txtpropertyname" runat="server"

                                   Text='<%# Bind("Nameofproperty") %>'>&l
t;/asp:Label>
                           </ItemTemplate>
                       </asp:TemplateField>













然后按照以下方式绑定您的代码;









then bind your code following way;

SqlConnection con=new SqlConnection(" U R connection string here:");
con.open();
SqlCommand cmd=new SqlCommand(" U R Query here.....",con);
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds,"a");
gridview1.DataSource=ds;
gridview1.DataBind();re>


这篇关于将数据绑定到模板字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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