Pages

Saturday, November 30, 2013

How to bind LINQ data to dropdownlist (TEXT,VALUE)


Let's assume we have a Web Service which will return the results as an
object with the values DDLTEXT
and
DDLVALUE.
The following will explain how to use LINQ to query and then bind the results to the

DropDownList:
  DataTextField and  DataValueField

ddl.DataSource = wsMethod.Contents.OrderBy(o=>o.DDLTEXT);

//specify result object values
ddl.DataTextField = "DDLTEXT";
ddl.DataValueField = "DDLVALUE";

ddl.DataBind();


How to bind LINQ data to DataGrid

Let's assume we have a Web Service which will return the results as an object for IN and OUT data records.
The following will explain how to use LINQ to query and then bind the results to the asp:DataGrid


C# code:
//query to get currunt month data ordered by LOCAL_TIME then TYPE
 var thisMonthData = eData.Contents
.OrderByDescending(o => o.LOCAL_TIME)
.OrderByDescending(o2 => o2.TYPE)
.Where(m => m.DATE.Substring(0, 6)
== DateTime.Today.ToString("yyyyMM", arCulture));
 
//Bind Data
eDataGrid.DataSource = thisMonthData;
eDataGrid.DataBind();
 
//query to get currunt month data ordered by LOCAL_TIME for TYPE=IN
var thisMonthDataIn = eData.Contents
.OrderByDescending(o => o.LOCAL_TIME)
.Where(r => r.TYPE == "IN")
.Where(m => m.DATE.Substring(0, 6) == DateTime.Today.ToString("yyyyMM", arCulture));
 
//Bind Data
eINDataGrid.DataSource = thisMonthDataIn;
eINDataGrid.DataBind();
 
//query to get currunt month data ordered by LOCAL_TIME for TYPE=OUT
var thisMonthDataOut = eData.Contents
.OrderByDescending(o => o.LOCAL_TIME)
.Where(r => r.TYPE == "OUT")
.Where(m => m.DATE.Substring(0, 6) == DateTime.Today.ToString("yyyyMM", arCulture));
 
//Bind Data
eOUTDataGrid.DataSource = thisMonthDataOut;
eOUTDataGrid.DataBind();

 

Markup:


                     
<asp:DataGrid CssClass="gvTbl" ID="eINDataGrid" runat="server"  AutoGenerateColumns="true">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle BackColor="#DAF1F2" HorizontalAlign="Right" />
<AlternatingItemStyle BackColor="#ECE9EA" />
<Columns>
</Columns>
</asp:DataGrid>
 
<asp:DataGrid CssClass="gvTbl" ID="eOUTDataGrid" runat="server"  AutoGenerateColumns="true">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle BackColor="#DAF1F2" HorizontalAlign="Right" />
<AlternatingItemStyle BackColor="#ECE9EA" />
<Columns>
</Columns>
</asp:DataGrid>
<asp:DataGrid CssClass="gvTbl" ID="eDataGrid" runat="server"  AutoGenerateColumns="true">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle BackColor="#DAF1F2" HorizontalAlign="Right" />
<AlternatingItemStyle BackColor="#ECE9EA" />
<Columns>
</Columns>
</asp:DataGrid>

Monday, November 25, 2013

Using CSS only to Change backcolor of GridView or any table rows onmouseover

 I just learned a technique of applying a hover effect to an HTML table’s rows with the help of CSS, without using a single line of JavaScript, C#, VB, … etc. code.. all you hace to do is to define a new class with :hover selector.. 

Row Number 1
Row Number 2
Row Number 3

Let's suppose you have the following styles in your CSS file:
.tableClass
{
font-family: Tahoma;
      font-size: 9pt;
      font-weight: normal;
      color: #3d3d3d;
}
.tableRow
{
color: #3d3d3d;
      background-color: #daf1f2;
}
.tableAlterRow
{
background-color: #ece9ea;
      color: #3d3d3d;
}

To give the hover effect onmouseover, just add your styles to the following:

.tableClass tr. tableRow:hover,
.tableClass tr. tableAlterRow:hover,
.tableRow:hover,
.tableAlterRow:hover
{
   background-color:#199dbf !important;
   font-weight: bold;
}



Easy, right ;) !?

Saturday, November 23, 2013

asp DataGrid Format Text Field as DateTime

Here I'm binding to a DataGrid on an asp page, the Database contains a text field that holds a date text on the format "yyyyMMdd" and I want to show the date in the format "day MonthName year",

All I had to do is adding to the Datagrid <Columns> a TemplateColumn with the following binding:

<# DateTime.ParseExact(DataBinder.Eval(Container.DataItem, "DATE_Field").ToString(), "yyyyMMdd", new System.Globalization.CultureInfo("ar-sa")).ToString("dd MMM yyyy", new System.Globalization.CultureInfo("ar-sa"))%>

For example, let's say the database stores "14350117" in the field DATE_Field as text, the above binding will let it appear as  on the page..