开发者

How to bind one column to gridview which is not present in database

开发者 https://www.devze.com 2023-03-16 13:00 出处:网络
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.

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

精彩评论

暂无评论...
验证码 换一张
取 消