Go to DotNetFunda.com
<<= Please see left side tutorials menu.


Silverlight Tutorials | Report a Bug in the Tutorial

Found interesting? Add this to:

| More

asp:GridView control
GridView control is a successor to the ASP.NET 1.X DataGrid control.
 
GridView control is a successor to the ASP.NET 1.X DataGrid control. It provides more flexibility in displaying and working with data from your database in comparison with any other controls. The GridView control enables you to connect to a datasource and display data is tabular format, however you have bunch of options to customize the look and feel. When it is rendered on the page, generally it is implemented through <table> HTML tag.

Its properties like BackColor, ForeColor, BorderColor, BorderStyle, BorderWidth, Height etc. are implemented through style properites of <tahle> tag.

Following are some important properties that are very useful.
Behavior Properties of the GridView Control
AllowPaging true/false. Indicate whether the control should support paging.
AllowSorting true/false. Indicate whether the control should support sorting.
SortExpression Gets the current sort expression (field name) that determines the order of the row.
SortDirection Gets the sorting direction of the column sorted currently (Ascending/Descending).
DataSource Gets or sets the data source object that contains the data to populate the control.
DataSourceID Indicate the bound data source control to use (Generally used when we are using SqlDataSource or AccessDataSource to bind the data, See 1st Grid example).
AutoGenerateEditButton true/false. Indicates whether a separate column should be added to edit the record.
AutoGenerateDeleteButton true/false. Indicates whether a separate column should be added to delete the record.
AutoGenerateSelectButton true/false. Indicate whether a separate column should be added to selecat a particular record.
AutoGenerateColumns true/false. Indicate whether columns are automatically created for each field of the data source. The default is true.
Style Properties of the GridView Control
AlternatingRowStyle Defines the style properties for every alternate row in the GridView.
EditRowStyle Defines the style properties for the row in EditView (When you click Edit button for a row, the row will appear in this style).
RowStyle Defines the style properties of the rows of the GridView.
PagerStyle Defines the style properties of Pager of the GridView. (If AllowPaging=true, the page number row appears in this style)
EmptyDataRowStyle Defines the style properties of the empty row, which appears if there is no records in the data source.
HeaderStyle Defines the style properties of the header of the GridView. (The column header appears in this style.)
FooterStyle Defines the style properties of the footer of GridView.
Appearance Properties of the GridView Control
CellPadding Indicates the space in pixel between the cells and the border of the GridView.
CellSpacing Indicates the space in pixel between cells.
GridLines Both/Horizontal/Vertical/None. Indicates whether GrdiLines should appear or not, if yes Horizontal, Vertical or Both.
HorizontalAlign Indicates the horizontal align of the GridView.
EmptyDataText Indicates the text to appear when there is no record in the data source.
ShowFooter Indicates whether the footer should appear or not.
ShowHeader Indicates whether the header should appear or not. (The column name of the GridView)
BackImageUrl Indicates the location of the image that should display as a background of the GridView.
Caption Gets or sets the caption of the GridView.
CaptionAlign left/center/right. Gets or sets the horizontal position of the GridView caption.
State Properties of GridView Control
Columns Gets the collection of objects that represent the columns in the GridView.
EditIndex Gets or sets the 0-based index that identifies the row currently to be edited.
FooterRow Returns a GridViewRow object that represents the footer of the GridView.
HeaderRow Returns a GridViewRow object that represents the header of the GridView.
PageCount Gets the number of the pages required to display the reocrds of the data source.
PageIndex Gets or sets the 0-based page index.
PageIndex Gets or sets the number of records to display in one page of GridView.
Rows Gets a collection of GridViewRow objects that represents the currently displayed rows in the GridView.
DataKeyNames Gets an array that contains the names of the primary key field of the currently displayed rows in the GridView.
DataKeys Gets a collection of DataKey objects that represent the value of the primary key fields set in DataKeyNames property of the GridView.
Events associated with GridView Control
PageIndexChanging, PageIndexChanged Both events occur when the page link is clicked. They fire before and after GridView handles the paging operation respectively.
RowCancelingEdit Fires when Cancel button is clicked in Edit mode of GridView.
RowCommand Fires when a button is clicked on any row of GridView.
RowCreated Fires when a new row is created in GridView.
RowDataBound Fires when row is bound to the data in GridView.
RowDeleting,RowDeleted Both events fires when Delete button of a row is clicked. They fire before and after GridView handles deleting operaton of the row respectively.
RowEditing Fires when a Edit button of a row is clicked but before the GridView hanldes the Edit operation.
RowUpdating, RowUpdated Both events fire when a update button of a row is clicked. They fire before and after GridView control update operation respectively.
Sorting, Sorted Both events fire when column header link is clicked. They fire before and after the GridView handler the Sort operation respectively.
DEMO : Button Show Source Code
1st Grid: Simple GridView with SqlDataSource
 Delete?AutoIDNameAddressPhoneCity
Edit Delete 2345aaaa
Edit Delete 2333abcdefghijlmnopqrstuvwxyzaaa
Edit Delete 2335Baas dddfsdf
Edit Delete 2329czcczczczczczczxczcczcz
Edit Delete 2315daaa
Edit Delete 2342dereer
Edit Delete 2343dereer
Edit Delete 2344dereer
12345678910...
Note: Updating this GridView will not affect 2nd GridView listing unless you come to this page without page postback as that GridView data has been bound in not IsPostBack condition.
2nd Grid: GridView with CodeBehind Data Bound
AutoIDNameAddressPhoneCity
2345aaaa
2333abcdefghijlmnopqrstuvwxyzaaa
2335Baas dddfsdf
2329czcczczczczczczxczcczcz
2315daaa
2342dereer
2343dereer
2344dereer
2317dfasfdasasdfasfadfsdsafasdfadsf
2336dfgdgdshsdh
2341dfsfd443fds
2327ffffffffffff
2346fvgjgfjfgjfgj
2331gfgvcvbvbbvc
2286ghdfnjklhk70897097
2282hjhgj876ghjhgj876hgjhgjghjhg
2310hjhikjkjjjjjj
2311jhkjjkjlkjkjlkjkjk
2321john1232381111troy
2323kegf
2328mannfdsf23213dsfdsf
2320ppppppp
2302Praveen Gurgaon78945612Gurgaon
2287qqqnjklhk70897097
1752qqqqqqtuticorinasdfasdf
2347qweqweqweqwe
2179qweweqwer14144134fdsgdsfg
1499rddfdffd
2074rajqqq
1945rajachennai222222222chennai
1472rajahyd2314ghfqwerwe
2061Rajesh142/12 ram gali9900112233ajmer
1578rajkumarGrean45454Karur
1538rajkumarashoksaralqrwe
2253Rakeshbangalore9611299277bangalore
1932RamJaipur9232332230Jaipur
1933RamJaipur9088989909Jaipur
1934Ramljkklkjl
2096ramindia123545chennai
1833RamanaReddyAmeerpet9703268938Hyderabad
1939RAT65234567890Chennai
1986rathorefdf24343dsdf
2100Raulmjn
1328ravindrakumarghghg598798798juhuh
2228rdfrdfrfff
1982reeererer
2222retertertertertertertert
2223retertertertertertertert
2224retertertertertertertert
2265rewrewrwrew
2266rewrwerwrwe
1904robinddddddd
1878rrrrrrrrrrrrrrrr
2215rtbjj0989blbl
2313rtertretwertertwertertwert
1667rtrrtrtrtrtrt
1992rtreerbangalore998659618Bangalore
2194rtreerrerterertererter
1990rtttttwsrttygtteyy
2263rttytytytytytytytytyyyuyu
1671rturtueurtzurtzuru
1432rwrwrrwrrw
1750rwererwerwerer
1948rwerwwrewrwrwrwrrwr
1739swaa
1741swaa
1318ssss
1332ss32322332
2299sasdasdasd
2192ssss
1987S23weqep
1708ssss
2283ssss
1886ssss
1630ssss
2115sasa123sa
1662sawweerr
1935sassssss
2021sadasdasdasd
1322sadasdasasdasasdas
1510sadasdasasdasdasdasd
2312sad   
2010sadfasdfasdfasdf
1361sadhanaandheri456781452andheri
1362sadhanadfa8989AFDASF
1894sadsadasdsadasdasdsadsafasfasfa
1969saerfersdsvsv133331vvdd
2155safasdfasfafafdsaf
2238safdasfsannnn516756315nhyghfghfghhh
2226sagarpatel9978265019ahmedabad
2167saiaaa
1596samanand9003992220tuty
2086samanhaw1232kursistan
2078Samsungasa121asa
1772sanjayhdgfhhdfghhdf
1830sanjaysanjaysanjaysanjay
1740saran12,asdsads sads231321312sfsdfsdfsd
1795sassas2121212ewew
1946sasabbb999jjj
1930satishpfc33333ssss
1426satishvskp81440085504vskp
2197saurabhaaaaaaaaa
2218SauravKolkata10000Kolkata
1733sdasdassadasds
2109sdsdasasss
2110sdsdasasss
2111sdsdasasss
2112sdsdasasss
1956sdsd??szDasd
1809sdsdf45rsdf
2113sdsdasasss
2195sdadadasdaasdaasda
2234sdassafdsasfdsfsafsdf
1458sddfdfdfdfdfdfd
1459sddfdfdfdfdfdfd
1462sddfdfdfdfdfdfd
1464sddfdfdfdfdfdfd
1330sddssaasaadd4343434343ddsdds
1379sdfsdfsdfsfd
1616sdfsdfsdfsdf
1661sdfdsfsdfsdf
1478sdfsdfsdfsdfsdfsdf
1571sdfasdfsafasd
2291sdfdsfdsfadf
1500sdfsadfsfdasfd
1392sdfsdfsdfsdf
2340sdfsfsdfsf
1451sdfsdfsdfsdf
1501sdfsadfsfdasfd3
1735sdfsdfsdfssdf
1924sdfdsfsdfsdf
1791sdfsdfsdfsdf
1793sdfyffhfgh
1725sdfhahahahahahabnmbnm
1697sdfsdfsdfsdf
1685sdfafsdfasdsfaffasdfasd
1686sdfafsdfasdsfaffasdfasd
1349sdfaasdfsdfaafsd
2098sdfasdfasdfasdffas
1393sdfgsdfg345345sdfg
2117sdfssdfsdfsdf
1974SDFSDSDFSD234234ASDFSDF
2187sdfsdfsdfsdafsdf
2035sdfsdfsdfsdfsdfsdfsdfdsf
1644sdfsdfasdfasd4534543asfsdfsdf
1973sdggsdgsdgsdgsd
1424SDNSDVSDFBJKVF1DSFASDAFSDAFFSDFAFDSAFSDFAFSDA
2275sdsdsdsdsdsdsdsdssdsd
2273sdsdsdsdsdsdsdsdssdsd
2276sdsdsdsdsdsdsdsdssdsd
2278sdsdsdsdsdsdsdsdssdsd
2271sdsdsdsdsdsdsdsdssdsd
2272sdsdsdsdsdsdsdsdssdsd
2274sdsdsdsdsdsdsdsdssdsd
2277sdsdsdsdsdsdsdsdssdsd
2279sdsdsdsdsdsdsdsdssdsd
2280sdsdsdsdsdsdsdsdssdsd
1669sdsdsdsdsdsdsdsd
1497sdsffdgfdg23344fdgfgf
1327sdvsdvsdvsdvsdvsdv
1796seemagdhsdghdfdshgdggh
1348sersdf4545454aafsd
2210setlimanila1234manila
2013sfdsfsd344dasfs
2190sfdsfsfdsf3343fd
1517sfssfsf122222
1555sfsafaala
1779shabkol3535chen
2209shariquepulgate9970848286pune
1658shilpapune213556vbbbbbbfg
1798shilpaetrter35435464365fdsfsdg
1316Shjreebangalore0989876787 
1325shreebangalore8766786868pune
1938shrikanthabsiguda9985822222hyd
1895shuchitabangalore  
2045Siju 9447367717Kochi
1611sowmyajayanagar45678899bangalore
2307sriaaaaaaaa9999999999aaaaaaaaa
1329Srikanthbnmbnmdfbn
2053srinivashyderabad9703268937hyderabad
2012srinivashyderabad867876786hyderabad
2212srinivassrinivassrinivassrinivas
1866srinivasameerpet687687768976hyderabad
1542srinivas chowdaryguntur786867868767hyderabad
2054srinivas chowdaryhyderabad9703268937hyderabad
1993srinivas chowdaryguntur'76868767865hyderabad
1346srinivasansfds13432sdfds
1344srinivasanasdadad
2180srinu12398833333333hyd
1486srtyysssasas
1543ssaa33ff
1663sssdffdfdf
1455ssssssss
2094sssaassaas
1531ssssdsds23232sdsd
2290ssssssssshhhhhhhhhhh555555555555555lllllllllll
1931sssssssssssssssssssssss33333333ssssssss
2161stuffwhowhatwhere
2042Subhadeepasad9985654778Kolkaa
2293suniluppal988875424hyd
1341sunilhyd555555555hhhhhhhhh
2027sunilzeea
2284sureshAddressPhoneCity
1617swehyd5555hyd
1903swwsafdasdfasdfdsf
1888tttt
1826tttt
2007TTTT
2325tttt
1889tfsdaffsdffsd
2036tdtreyttyrytytreytre
1922testtest12323garde
1423testtesttesttest
2309testtesttestTEST
1528testttt
1687TestTRest2356666Test
2204testwqeweeqwe
1880testtesttesttest
1711TestTest909099090090pune
2319test88 djo dr102-193-1292Loa
2305testtesttesttest
1597testfszfsffsfsffsfsf
2149TestTestTestTest
2231testtest55555555test
1634testtesttesttest
1386testtest3454543trest
2139testtest123123test
2141testtesttesttest
2164testjkdkjdfsfgfgfdgfgdhdfjk
1339TestTtTtgtt
1744testtest99999999city
1861testtesttesttest
2022Test`TestTestTest
1893test1test2test3test4
2171testetestetesteteste
1926TESTETESTE91189050BELEM
2173testetestetesteteste
2032Testingzeea
1736testtetesttesttest
1836tiyak12k12k12
2292trrdffdfds
1358trytuytuytu
1601tryryrytrtyryttryrytry
1530tttttttt
1704tthhhhh
2193ttryrtytrytrytrytrtrhytrytr
2132tttdsfds4353ff
1608tttttssss343cxcxc
2257TYTYTYT   
2189tyukghjksdf45676 
1485tyutyufghfghfg6646hfghf
1817uiasdfasdfadf
1789uioioioiiuo
1683USAnjomwqfUSAUSA
1777USAiubuhlsbUSAUSA
1670USAtjvghhmUSAUSA
2102UshaDelhi00Delhi
1967uttuutdtuddgdfgdfg
1968utuutduuddugdgd
1609uuuussss445454545bvbvbv
1610uuuussss445454545bvbvbv
1626uuuuu7uuuuu88888iokiii
1981uyyyy66hh
1400uyyyueateatatea
1848vddd
1722varora4545454chennai
1727varora4545454chennai
2099Varun123 Main228-2222Here and Now
1646varunmig94kp
2072vcvcxvxcvcx
2120vffff
2118vffff
2119vffff
2121vffff
2245vijayameerpet9948969626hyderabad
2153vilasBangalore7474747474Bangalore
2039vimalnoida8800615290noida
2077wwww
2060wwww
2339wefsdfsfsf
2174wergergergregregreg
2188werwerwerwe343434efwer
2348wqweqweqweqwe
2133wrwrewrwaaaaaaaaaa
2244xvxcvxcv43xcvxcv
2260xvxcvxvxcvxcv
2322xxx111111111ttttt
2075yuiuyiuyiuiuyi
2126YuorajPune-149090pune
2146yuvipune-149999pune
2205yuytuytutyutyutyuytu
2029yuyuzeea
2064zenosahaya84942trere
2145zxsasss
                
// 1st GridView /////////////////////////
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" Caption="1st Grid: Simple GridView with SqlDataSource"
     AllowPaging="True" AllowSorting="True" AutoGenerateEditButton="true" 
      DataKeyNames="AutoID" PageSize="8">
      <Columns>
        <asp:TemplateField HeaderText="Delete?">
            <ItemTemplate>
                <asp:LinkButton ID="lnk1" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure to Delete?')" CommandName="Delete"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
      </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:ConnStr %>'
     SelectCommand="Select * FROM SampleForTutorials ORDER BY [Name]"
     DeleteCommand="Delete FROM SampleForTutorials WHERE AutoID = @AutoID"
     UpdateCommand="UPDATE SampleForTutorials SET Name = @Name, Address = @Address, Phone = @Phone, City = @City WHERE AutoID = @AutoID">
        <DeleteParameters>
            <asp:Parameter Name="AutoID" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="AutoID" Type="Int32" />
            <asp:Parameter Name="Name" Type="string" Size="50" />
            <asp:Parameter Name="Address" Type="string" Size="200" />
            <asp:Parameter Name="Phone" Type="int32" />
            <asp:Parameter Name="City" Type="string" Size="20" />
        </UpdateParameters>
     </asp:SqlDataSource>
     
     
     // 2nd GridView /////////////////////////
<asp:GridView ID="GridView2" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
 Caption="2nd Grid: GridView with CodeBehind Data Bound" AllowPaging="False" AllowSorting="False"
    BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">
    <FooterStyle BackColor="Tan" />
    <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
    <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" Font-Bold="True" />
    <HeaderStyle BackColor="Tan" Font-Bold="True" />
    <AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>     
 
 
 // CODE BEHIND
 
// Bind the GridView /////////////////////////
private void BindGridView()
{
    string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
    SqlConnection conn = new SqlConnection(connStr);
    conn.Open();
    SqlDataAdapter dAd = null;
    DataSet dSet = new DataSet();
    try
    {
        string strSql = "Select * FROM SampleForTutorials ORDER BY [Name]";
        dAd = new SqlDataAdapter(strSql, conn);
        dAd.Fill(dSet, "SampleTable");

        GridView2.DataSource = dSet.Tables["SampleTable"].DefaultView;
        GridView2.DataBind();
        
    }
    catch (Exception ee)
    {
        lblError.Text = "Error occured. <br>+" + ee.Message.ToString();
    }
    finally
    {
        dSet.Dispose();
        dAd.Dispose();
        conn.Close();
        conn.Dispose();
    }
}
     
                    




About Us | The Team | Advertise | Contact Us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 9/3/2010 4:06:07 AM