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)

No comments:

Post a Comment