简体   繁体   中英

Append GridView-Columns after DataTable was databound

I have a DataTable which I have created in code file and I later bind it to a GridView which i have created by using the drag and drop feature of Visual studio.

However I have added some columns in the GridView which are not supported(correct me if I am wrong) in a DataTable , eg Hyperlink-Column or CheckBox-Column.

I would like to build the hyperlink column and the checkbox id's with a value derived from a value generated in the DataTable from a particular column. I know I can use DataNavigateUrlfields to build dynamic links but how do I do this with a DataTable that is to be bound later?

I also need the columns in the GridView to appear after the columns in the DataTable .

Any help in the above will be highly appreciated. Any alternatives are also appreciated.

Declaratively added controls will be created first and then the databound/manually created(documented in the Page's Lifecycle , s‌​earch for "controls created declaratively" ). Since you want the declarative columns last, you need a hack:

You could use RowDataBound to change the order, so that the AutoGenerated columns are followed by your other columns like Hyperlink or CheckBox columns:

protected void gridOffers_RowDataBound(object sender, GridViewRowEventArgs e)
{
    TableCell cell = e.Row.Cells[0];
    e.Row.Cells.RemoveAt(0);
    //Move first to the end
    e.Row.Cells.Add(cell);
    cell = e.Row.Cells[0];
    e.Row.Cells.RemoveAt(0);
    //Move second to the end
    e.Row.Cells.Add(cell);
}

However I have added some columns in the GridView which are not supported(correct me if I am wrong) in a DataTable, eg Hyperlink-Column or CheckBox-Column.

They don't need to be supported in the DataTable but in the GridView. The table contains your data and the grid contains the view.

You can try with this code - based on RowDataBound

<Columns>
       <asp:TemplateField>
              <ItemTemplate>
                  <asp:HyperLink ID="HyperLink1" runat="server" Text=""></asp:HyperLink>
              </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField>
              <ItemTemplate>
                  <asp:CheckBox ID="CheckBox1" runat="server" />
              </ItemTemplate>
       </asp:TemplateField>

</Columns>

You can adjust you datbound with your event RowDataBound, i added HyperLink control, in order to customize your link as you want.

 void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
 {
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      var hyperLink = (HyperLink)e.Item.FindControl("HyperLink1");
      hyperLink.NavigateUrl ="...."; 

      var checkBox = (CheckBox)e.Item.FindControl("CheckBox1");
      checkBox.Checked =....


    }

  }

Well there is an easy way to append a column if you are using datatable

DataTable yourDataTable = new DataTable();

Load yourDataTable by DataReader or DataAdapter

DataColumn newColumn = yourDataTable.Columns.Add("Total", typeof(Int32));

After that you can bind this datatable into your Gridview. msdn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM