【问题标题】:how to make modifications in a gridview如何在网格视图中进行修改
【发布时间】:2009-12-15 17:06:37
【问题描述】:

我有一个这样的 gridview 表...

<div>

  <asp:GridView ID="GridView1" runat="server" 
    AllowSorting="true" 
    OnSorting="TaskGridView_Sorting" >
  </asp:GridView>

</div>

我正在用 2 个数组列表填充 gridview,如下所示

DataTable taskTable = new DataTable("TaskList");


            taskTable.Columns.Add("File Name");

            taskTable.Columns.Add("Failure Count");

            for (int i = 0; i < namesOfFiles.Count; i++)
            {
                DataRow tableRow = taskTable.NewRow();
                tableRow["File Name"] = namesOfFiles[i];
                tableRow["Failure Count"] = buFailureCount[i];
                taskTable.Rows.Add(tableRow);
            }
            Session["TaskTable"] = taskTable;

           GridView1.DataSource = Session["TaskTable"];
           GridView1.DataBind();

所以现在当我运行它时,我在屏幕上看不到任何东西,除非我将自动生成列属性设置为 true....

有没有一种方法可以获得模板字段,因为我知道很多方法可以在 gridview 中修改数据,或者这些列在我的代码中对齐的方法。现在标题和项目卡在左边距...

谢谢

【问题讨论】:

    标签: c# asp.net gridview sorting


    【解决方案1】:

    是的,您可以使用BoundField 元素轻松获取模板字段。还有ItemTemplate 元素可以更好地控制网格视图。 MSDN's tutorial 应该会给你所有你需要知道的。

    实际上你的代码会是这样的:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID" DataSourceID="ObjectDataSource1">
        <Columns>
            <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
            <asp:BoundField DataField="HireDate" HeaderText="HireDate" SortExpression="HireDate" />
        </Columns>
    </asp:GridView>
    

    要设置网格视图的样式,您需要查看 &lt;RowStyle /&gt; 元素。

    【讨论】:

      猜你喜欢
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 2013-12-08
      相关资源
      最近更新 更多