Showing posts with label Gridview. Show all posts
Showing posts with label Gridview. Show all posts

Tuesday, April 16, 2013

Asp.Net GridView useful tips and tricks

Asp.Net GridView useful tips and tricks

OUTSIDE GRIDVIEW CODE BEHIND

//Filtering only just DataRow inside GridView RowDataBound (i.e. Excluding Header, Footer and Pager)
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Finding Control inside the GridViewRow
 Button b = ((Button)e.Row.FindControl("MyButtonID"));

//Get Data Item field (Might need to validate as your own)
string CustomerID = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "CustomerID"));


//Get GridViewRow in the button click of GridView ItemTemplate
Method 1) GridViewRow gvr = (GridViewRow)(sender as Button).NamingContainer;
Method 2) GridViewRow gvr = (sender as Button).Parent.Parent as GridViewRow;

//Get Parent's GridViewRow in the button click of Nested GridView ItemTemplate
GridViewRow ParentRow = e.Row.Parent.Parent.Parent.Parent as GridViewRow;

INSIDE GRIDVIEW

//Add auto increase Sr. No. (1,2,3,4.....)
<%#Container.DataItemIndex+1 %>

//Binding Data Item
Text='<%#Eval("CustomerID")%>'

//Binding Multiple Data Items
Text='<%#Eval("Key")+"|"+Eval("Line_No") %>'

//Binding Data Item with Custom Text

Text='<%#"Customer ID is " + Eval("CustomerID")%>'

//Add Codebehind Class property inside the GridView
<%# CodeBehindProperty %>

//Adding HyperLinkField

<asp:HyperLinkField HeaderText="Customer ID" DataTextField="CustomerID" DataNavigateUrlFormatString="CustomerDetail.aspx?c=y&amp;id={0}&amp;name={1}" DataNavigateUrlFields="CustomerID,CustomerName" ControlStyle-CssClass="blulink">

 //Adding HyperLink Control in ItemTemplate
<asp:HyperLink ID="HyperLink1" runat="server"  navigateurl='<%# String.Format("~/GLEntryDetails.aspx?uid={0}&section={1}&des={2}&Field4=something", Eval("UserID"), Eval("Section"), Eval("Description")) %>' Text='<%#Eval("Click Here") %>'></asp:HyperLink>

//BoundField Data Format of Date

<asp:BoundField DataFormatString="{0:dd/MM/yyyy}" />

//Item Template Data Format of Date
<asp:ItemTemplate><asp:Label Text='<%# Bind("Expiry_Date","{0:dd-MM-yyyy}") %>'/></asp:ItemTemplate>

//BoundField Data Format of Currency
Method 1) DataFormatString="{0:#,###.00}"
Method 2) DataFormatString="{0:N2}"
Method 3) DataFormatString="{0:dd/MM/yyyy hh:mm:ss tt}"

Monday, February 25, 2013

Select or Deselect all by checkbox in Gridview

By JQuery

<script type="text/javascript">
$(document).ready(function() {

var headerCheckbox = $('#GridView1 > tbody > tr > th > input:checkbox');

headerCheckbox.click(function() {
var headerChecked = $(this).attr('checked');
var rowCheckboxes = $('#GridView1 > tbody > tr > td > input:checkbox');
rowCheckboxes.attr('checked', headerChecked);
});

});
</script>
Go to complete example


By classic javascript 

<script type="text/javascript">
function CheckAllRow(chkBox) {
var gridViewCtlId = '<%=GridView1.ClientID%>';
var grid = document.getElementById(gridViewCtlId);
var gridLength = grid.rows.length;
for (var i = 1; i < gridLength; i++) {
cell = grid.rows[i].cells[1];
for (var j = 0; j < cell.childNodes.length; j++) {
if (cell.childNodes[j].type == 'checkbox') {
cell.childNodes[j].checked = chkBox;
}
}
}
}
</script>
Go to complete example

Tuesday, February 5, 2013

Gridview Row Mouseover highlight and anchor link


 protected void Gridview_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblDreceive = ((Label)e.Row.FindControl("lblDateReceive"));
            Label lblDissue = ((Label)e.Row.FindControl("lblDateIssue"));

            object d1 = DataBinder.Eval(e.Row.DataItem, "Posted_Purch_Receipt_Date");
            object d2 = DataBinder.Eval(e.Row.DataItem, "Sales_Shipment_Date");
            if (d1 != null)
            {
                DateTime dReceive = DateTime.Parse(d1.ToString());
                lblDreceive.Text = dReceive.Year > 1999 ? dReceive.Day.ToString("00") + "-" + dReceive.Month.ToString("00") + "-" + dReceive.Year.ToString("00") : "";
            }
            else
            {
                lblDreceive.Text = "";
            }

            if (d2 != null)
            {
                DateTime dIssue = DateTime.Parse(d2.ToString());
                lblDissue.Text = dIssue.Year > 1999 ? dIssue.Day.ToString("00") + "-" + dIssue.Month.ToString("00") + "-" + dIssue.Year.ToString("00") : "";
            }
            else
            {
                lblDissue.Text = "";
            }

            string script = String.Format("window.open('./EditBufferExchange.aspx?t=tool&entryno={0}&purrec={1}','_self')", DataBinder.Eval(e.Row.DataItem, "Entry_No"), DataBinder.Eval(e.Row.DataItem, "Posted_Purch_Receipt_No"));
            e.Row.Attributes.Add("onclick", script);

            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#F9E5B6';this.style.cursor='pointer';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff';");
            }
            else
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#F9E5B6';this.style.cursor='pointer';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#f1f3f6';");
            }


        }
    }