views:

34

answers:

1

i have a gridview table like this...

<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

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

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

thanks

+1  A: 

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.

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.

Gavin Miller