gridview中的EmptyDataText未按预期正确显示 [英] EmptyDataText in gridview not properly displayed as expected
本文介绍了gridview中的EmptyDataText未按预期正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 场景
当gridview中没有数据时,会显示消息但是......但消息显示在表/面板中。
但我不想在面板/桌子里面......
代码如下< br $>
--Scenario
When there is no data in the gridview then the message is displayed but ...but the message is displayed within a table/panel.
But i dont want that inside a panel/table...
code is as below
<div class="searchResult">
<asp:UpdatePanel ID="UpdatePanel12" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gridShuttleAdmin" runat="server" EmptyDataText="No Records Found..."
AutoGenerateColumns="false" DataKeyNames="Shuttle_Facility_Id" OnRowDataBound="gridShuttleAdmin_RowDataBound"
OnRowDeleting="gridShuttleAdmin_RowDeleting" OnRowEditing="gridShuttleAdmin_RowEditing"
OnRowUpdating="gridShuttleAdmin_RowUpdating" OnRowCancelingEdit="gridShuttleAdmin_RowCancelingEdit">
<SelectedRowStyle CssClass="ResultSelectedRow" BackColor="Cyan" />
<RowStyle CssClass="ResultRow" BackColor="#EFF3FB" />
<PagerStyle BackColor="#999999" ForeColor="AliceBlue" HorizontalAlign="Center" />
<HeaderStyle CssClass="searchResultHeader" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="Bisque" />
<Columns>
<asp:TemplateField HeaderText="Trip Number">
<ItemTemplate>
<asp:Label ID="lbltrip" runat="server" Text='<%#Eval("Trip_num") %>'></asp:Label>
<asp:Label ID="lblshuttlefacilityid" runat="server" Visible="false" Text='<%#Eval("Shuttle_Facility_Id") %>'></asp:Label>
<asp:Label ID="lblfacilityid" runat="server" Visible="false" Text='<%#Eval("facility_id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtshuttletrip" ReadOnly="true" CssClass="textBox" runat="server"
Text='<%#Bind("Trip_num") %>'> </asp:TextBox>
<asp:Label ID="lblshuttlefacilityid" runat="server" Visible="false" Text='<%#Eval("Shuttle_Facility_Id") %>'></asp:Label>
</EditItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Shuttle Detail Name">
<ItemTemplate>
<asp:Label ID="lblshuttledetailname" runat="server" Text='<%#Eval("Shuttle_Facility_DetailName") %>'>
</asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
<EditItemTemplate>
<asp:TextBox ID="txtfacilityname" Width="250px" CssClass="textBox" runat="server"
Text='<%#Bind("Shuttle_Facility_DetailName") %>'> </asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Seats Avaliable">
<ItemTemplate>
<asp:Label ID="lblseats" runat="server" Text='<%#Eval("Shuttle_Seats") %>'>
</asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
<EditItemTemplate>
<asp:TextBox ID="txtshuttleseats" CssClass="textBox" runat="server" Text='<%#Bind("Shuttle_Seats") %>'> </asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Shuttle Number">
<ItemTemplate>
<asp:Label ID="lblshuttlenum" runat="server" Text='<%#Eval("Shuttle_Number") %>'>
</asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
<EditItemTemplate>
<asp:TextBox ID="txtshuttlenumber" CssClass="textBox" runat="server" Text='<%#Bind("Shuttle_Number") %>'> </asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Add Shuttle Details">
<ItemTemplate>
<asp:LinkButton ID="lnkviewID" ForeColor="ActiveCaption" Text="Add/Edit" CommandName="View"
runat="server"></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="linkedit" ForeColor="ActiveCaption" runat="server" Text="Edit"
CommandName="edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkupdate" runat="server" Text="update" CommandName="update" />
<asp:LinkButton ID="lnkcancel" runat="server" Text="cancel" CommandName="cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="lnkDeleteID" ForeColor="ActiveCaption" Text="Delete" CommandName="Delete"
OnClientClick="return confirm('Are you sure to delete?')" runat="server"></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
推荐答案
Ok So this is the real Problem
try this
Ok So this is the real Problem
try this
// method for showing the Header and Footer of GridView when no data returned in the query
// You may not use it in that case no header and footer will be shown
private void ShowNoResultFound(DataTable source, GridView gv)
{
source.Rows.Add(source.NewRow()); // create a new blank row to the DataTable
// Bind the DataTable which contain a blank row to the GridView
gv.DataSource = source;
gv.DataBind();
// Get the total number of columns in the GridView to know what the Column Span should be
int columnsCount = gv.Columns.Count;
gv.Rows[0].Cells.Clear();// clear all the cells in the row
gv.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
gv.Rows[0].Cells[0].ColumnSpan = columnsCount; //set the column span to the new added cell
//You can set the styles here
gv.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
gv.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red;
gv.Rows[0].Cells[0].Font.Bold = true;
//set No Results found to the new added cell
gv.Rows[0].Cells[0].Text = "NO RESULT FOUND!";
}
// now you have to call this method when DataSource returns nothing.
private void BindGridView()
{
DataTable dt = new DataTable(string user);
SqlConnection connection = new SqlConnection(GetConnectionString());
try
{
connection.Open();
string sqlStatement = "SELECT* FROM Orders WHERE UserID = @User";
SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
sqlCmd.Parameters.AddWithValue("@User", user);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0) //Check if DataTable returns data
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
Else //else if no data returned from the DataTable then
{ //call the method ShowNoResultFound()
ShowNoResultFound(dt,GridView1);
}
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
connection.Close();
}
}
add this in your client side code
add this in your client side code
<EmptyDataTemplate>
Data not available
</EmptyDataTemplate>
For more details see this GridView.EmptyDataTemplate Property
use this
<emptydatatemplate>No Records Found!!!</emptydatatemplate>
use this
<emptydatatemplate>No Records Found!!!</emptydatatemplate>
这篇关于gridview中的EmptyDataText未按预期正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文