How to display user friendly date formate in Gridview ?


Hi
So many time we will get the scenario to display user friendly date format in gridview, Datalist or Repeater control.

We can do format the Gridview template like this

<asp:TemplateField HeaderText="Posted date">
<ItemTemplate>
<asp:Label ID="lblpostedDate" runat="server" Text='<%#Eval("PostedDate","{0:dd/MM/yyyy}") %>’ />
</ItemTemplate>
</asp:TemplateField>

Advertisement

How to display Null field in Gridview ?



Hi

So many time we will get situation to display to some user friendly message in gridview if there is no data available in database.

We can do like this

Step1: Populate the gridview using any method

Step2: Customize the bound field like this

<asp:BoundField DataField="EmpName" NullDisplayText="No Data" HeaderText="EmpName" />
<asp:BoundField DataField="EmpSal" HeaderText="EmpSal" NullDisplayText="No Data" />

Complete Gridview code like this

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="Both"
CellPadding="4" ForeColor="#333333" Width="356px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="SI.No">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmpName" NullDisplayText="No Data" HeaderText="EmpName" />
<asp:BoundField DataField="EmpSal" HeaderText="EmpSal" NullDisplayText="No Data" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>

How to do joining of two table in EF ?


Hi
So many time we will get situation to join two table on basis of some condition. If we have to do inner join in Entity Framework then we can do like this

using (TestEntities TE = new TestEntities())
{
var query = from m in TE.tblEmps
join d in TE.tblDepts on m.Id equals d.Id
select new {m.EmpName,d.DeptName};

GridView1.DataSource = query;
GridView1.DataBind();

}