如何在gridview中进行修改

时间:2022-08-25 17:26:08

i have a gridview table like this...

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

<div>

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

</div>

i am populating the gridview with 2 arraylists like following

我正在使用2个arraylists填充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();

so now when i run this i dont see anything on the screen unless i put the autogenerate column property as true....

所以现在当我运行这个我没有在屏幕上看到任何东西,除非我把autogenerate列属性为真....

is there a way i can get template fields coz i know many ways to modify data in gridview then, or a way for these columns to be aligned in my code behind.. as now the header and the items are stuck on the left margin...

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

thanks

谢谢

1 个解决方案

#1


1  

Yes you can easily get template fields by using the BoundField element. There also exists ItemTemplate elements for greater control of the grid view. MSDN's tutorial should give you all you need to know.

是的,您可以使用BoundField元素轻松获取模板字段。还存在ItemTemplate元素,以便更好地控制网格视图。 MSDN的教程应该为您提供所有您需要知道的知识。

Effectively your code will come out something like this:

有效地,您的代码将出现如下:

<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>

To style your grid view you'll want to take a look at the <RowStyle /> element.

要设置网格视图的样式,您需要查看 元素。

#1


1  

Yes you can easily get template fields by using the BoundField element. There also exists ItemTemplate elements for greater control of the grid view. MSDN's tutorial should give you all you need to know.

是的,您可以使用BoundField元素轻松获取模板字段。还存在ItemTemplate元素,以便更好地控制网格视图。 MSDN的教程应该为您提供所有您需要知道的知识。

Effectively your code will come out something like this:

有效地,您的代码将出现如下:

<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>

To style your grid view you'll want to take a look at the <RowStyle /> element.

要设置网格视图的样式,您需要查看 元素。