Tuesday, November 18, 2014

Asp.net Binding


Asp.net Data binding that provides much easy way to manipulate data to the control eg.gridview,dropdownlist etc
I’ve notice when we are binding there are some unnecessary codes
 Dropdownlist1.DataTextField = "JObName";
 Dropdownlist1.DataValueField = "JobID"
 Dropdownlist1.DataSource = lstJobs;
 Dropdownlist1.DataBind();
We’re calling DataBind() method to asking to binding.totaly 4 lines of codes is required to bind dropdownlist.what is if we have common method to call to binding.
to find a way to overcome this issue in mind, I thought to create common method to bind it .oh no situation getting will be worsen , have to be specified to each of the  control to bind. Generic is there to solve my issue.
I wrote generic extension method to bind all controls…..
Here is the code…
    public static class EasyBinds
    {
        public static void EasyBind<T>(this Control ct, T t,   string ValueMember = "", string DisplayMember = "")
        {
            Utility<T>.CustomBind(ct, t,  ValueMember, DisplayMember);
        }
    }

    public static class Utility <T2>
    {
        public static void CustomBind(Control gv, T2 lst, string display = null, string value = null)
        {
            if (gv is GridView)
            {
                ((GridView)gv).DataSource = lst;
                ((GridView)gv).DataBind();
            }
            else if (gv is DropDownList)
            {
                ((DropDownList)gv).DataTextField = display;
                ((DropDownList)gv).DataValueField = value;
                ((DropDownList)gv).DataSource = lst;
                ((DropDownList)gv).DataBind();
            }
            else if(gv is ListBox)
            {
                ((ListBox)gv).DataTextField = display;
                ((ListBox)gv).DataValueField = value;
                ((ListBox)gv).DataSource = lst;
                ((ListBox)gv).DataBind();
            }
            else
            {
                throw new Exception("Invalid control to bind");
            }
        }
    }

Calling to method
GridView1.EasyBind<List<Employee>>(lst);
DropDownList2.EasyBind<List<Job>>(lstJobs, "JObName", "JobID");
ListBox1.EasyBind<List<Job>>(lstJobs, "JObName", "JobID");

Happy coding...

Monday, November 17, 2014

Working With Asp.net GRIDVIEW

Lets play with asp.net GRIDVIEW
hi guys, today I’m going to demonstrate working with grid view with different situations.asp.net provides much more flexible way to (tabular) to represent data  .keep it mind what every server tags finally converted to the classical html code (GRIDview to html table).
What is the easiest way to bind grid view?
1)Using Automaically Genarated columns
This method directly bind all the data source columns into grid.(good for only display data)
2)BoundField
If you’re not working data key fields in bound filed then you can’t able to get the selected cell value using column name , have to work with index?
Working with index like calling to trouble????????????????????
So what is the better way?
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  DataKeyNames="EmpNo,EmpName"
        ShowHeaderWhenEmpty="True">
        <Columns>
            <asp:BoundField HeaderText="EmpNO" DataField="EmpNO" />
            <asp:BoundField HeaderText="EmpName" DataField="EmpName" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server"        >
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EmptyDataTemplate>
            [Data not found]
        </EmptyDataTemplate>
    </asp:GridView>
Above code snippet shows in yellow color, to mark the column name as data key names. Then we can access the columns using data key names.
Eg. Int empID=(int)GridView1.DataKeys[0]["EmpNo"];

3)Template Filed
If we are using totally template fieds like lable,textbox it also very secure (can access data via its id) way to working with data, but it take times to develop. In exceptional scenario like add dropdown list then you’ll have to use it.

Here with I’ve attached code which contained working with selected dropdown item in grid view and events.

Happy coding.
(thre're more alternative way can find on internet)