How to bind one column to gridview which is not present in database
How to bind one column to the gridview which is not present in the database?
I want to display the total unit in the last column named Total Unit
but it is not present in database.
I got an argument exception:
开发者_开发百科Column 'tunit' does not belong to table.
foreach(DataRow row in dt.Rows )
{
object[] obj=new object[2];
obj[0] = row["Transaction_Id"];
obj[1] = row["tunit"];
dtgrid.Rows.Add(obj);
}
The best solution to this problem is to implement unbound column as it is explained in the Unbound Columns topic.
if you are binding a list, you can add a property to that list and bind to the grid using eval("PropertyName")
When you prepare the select query make sure you have one more extra column with the total value you want for that particular row, then you can specify a column in the gridview as follows, make sure you specify
'AutoGenerateColumns = false;
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="sds_datasourceName">
<Columns>
<asp:TemplateField HeaderText="Total" >
<itemtemplate>
<asp:Label ID="Total" runat="server" Text='<%# Bind("Total") %>'></asp:Label>
</itemtemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
精彩评论