如何将函数返回的值插入GridView? [英] How can I insert the values returned by a function into a GridView?

查看:66
本文介绍了如何将函数返回的值插入GridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表:table1(4列和10行),我必须使用一个函数来填充该table1中的数据。请帮忙。如果我创建一个标量函数,并使其逐行返回4个变量,我该如何使用这些值填充网格视图?

I have a table: table1(4 cols and 10 rows), I have to use a function to populate a gridview by the data in that table1. Please help. If I make a scalar function, and make it return 4 variables row by row, how do i go about populating the grid view using those values?

推荐答案

我在这里假设......好吧,一切。所以,有这样的网格视图:



标记:

Here I am assuming...well, everything. so, have a gridview like this:

Markup:
<asp:gridview runat="server" id="gridView1" autogeneratecolumns="false" emptydatatext="No records found." xmlns:asp="#unknown">
            <Columns>
                <asp:TemplateField HeaderText="col1">
                    <ItemTemplate>
                        <asp:TextBox runat="server" ID="tbCol1" Text='<%#Eval("col1") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="col2">
                    <ItemTemplate>
                        <asp:TextBox runat="server" ID="tbCol2" Text='<%#Eval("col2") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="col3">
                    <ItemTemplate>
                        <asp:TextBox runat="server" ID="tbCol3" Text='<%#Eval("col3") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="col4">
                    <ItemTemplate>
                        <asp:TextBox runat="server" ID="tbCol4" Text='<%#Eval("col4") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:gridview>





使用此代码填充gridview :

(示例是在页面加载但你需要的地方)



Populate the gridview with this code:
(example is on page load but do where you need to)

protected void Page_Load(object sender, EventArgs e)
       {
           // Create data table to fill with data from query.
           System.Data.DataTable dataTable = new System.Data.DataTable("myTable");

           // If you server is on local machine then Data Source=.;
           // If it is SQL Express on local machine then Data Source=.\SQLEXPRESS;
           string connectionString = @"Data Source=server_name;Initial Catalog=database_name;User Id=username;Password=password";
           // Using statement initialises object and auto disposes.
           using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(
               connectionString))
           {
               string selectQuery = "SELECT col1,col2,col3,col4 FROM table1";

               // Set data adapter which will send query to server and fill data table.
               System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(selectQuery, connection);

               connection.Open();
               da.Fill(dataTable);
               connection.Close();
           }

           gridView1.DataSource = dataTable;
           gridView1.DataBind();
       }


这篇关于如何将函数返回的值插入GridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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