没有数据时绑定GridView的问题 [英] Problem in Binding GridView when no data is present
本文介绍了没有数据时绑定GridView的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
i am绑定网格视图,当数据存在时,如果视图中没有数据,我必须从表中绑定它...
它在视图中存在数据时工作正常....但是当视图为空时,Eval函数抛出异常。
以下是GridView的代码: -
Hi all,
i am binding gridview from the view when data is present and if no data is presnt in view i have to bind it from a table..
it works fine when data is present in view....but when view is blank Eval function throws exception.
following is the code for the GridView:-
<asp:GridView ID="grid_Demand" runat="server" Width="80%" DataKeyNames="Book_Depot_Id_N"
AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
GridLines="None">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>Depot Id</HeaderTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<%# Eval("Book_Depot_Id_N")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Depot Name</HeaderTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<%# Eval("Book_Depot_Name_V") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Govt. Supply</HeaderTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:TextBox ID="txtGovt" runat="server" CssClass="textfield1" Text=''<%# Eval("Govt_Supply_Qty_N") %>''></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Private Supply</HeaderTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:TextBox ID="txtPvt" runat="server" CssClass="textfield1" Text=''<%# Eval("Pvt_Supply_Qty_N") %>''></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
以下是代码隐藏的代码: -
and the following is the code for code-behind:-
mEntity.BOOK_CLASS_ID_N = Convert.ToInt32(ddlclass.SelectedValue.ToString().Trim());
mEntity.Book_Id_N = Convert.ToInt32(ddlbookcode.SelectedValue.ToString().Trim());
dt = mAction.LoadWithWhereClause("PSS_GET_PRODUCTION_DEMAND_V", Constant.BOOK_CLASS_ID_N + " = " + mEntity.BOOK_CLASS_ID_N + " AND " + Constant.Book_Id_N + " = " + mEntity.Book_Id_N + " AND " + Constant.FinYear_Id_N + " = " + UtilsSession.GetCurrentFinYearId(), Constant.Book_Id_N);
if (dt.Rows.Count > 0)
{
// dt = mAction.Load(Constant.PSS_BOOK_DEPOT_T);
grid_Demand.DataSource = dt;
grid_Demand.DataBind();
}
else
{
dt = mAction.Load(Constant.PSS_BOOK_DEPOT_T);
grid_Demand.DataSource = dt;
grid_Demand.DataBind();
}
推荐答案
dt.Rows.Add(dt.NewRow());
grid_Demand.DataSource = dt;
grid_Demand.DataBind();
int colcount = grid_Demand.Rows[0].Cells.Count;
grid_Demand.Rows[0].Cells.Clear();
grid_Demand.Rows[0].Cells.Add(new TableCell());
grid_Demand.Rows[0].Cells[0].ColumnSpan = colcount;
grid_Demand.Rows[0].Cells[0].Text = "No record Present !.";
如果有帮助,请选择答案..
代码块添加sms-spelling已更正[/ edit]
Hey Nelek,谢谢建议,但我自己通过在将数据绑定到gridview之前添加列来解决它:
Hey Nelek, thanks for the advice, but i have solved it myself by adding columns to tha datatable before binding it to gridview:
dt.columns.add("column_name", typeof(string));
这篇关于没有数据时绑定GridView的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文