borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Problem displaying images from  database)Next Topic (why web user control can not run independly ?) New Topic New Poll Post Reply
AndreaVB Forum : ASP.Net : how to create and bind datagrid writing code in asp.net
Poster Message
tri_inn
Level: Regular User
Registered: 26-08-2002
Posts: 395

icon how to create and bind datagrid writing code in asp.net

please tell me how could i create datagrid writting code which will have feature like sorting,paging,in-place editing, at runtime column add and remove.
please guide me with source code.

05-07-2004 at 05:08 AM
View Profile Send Email to User Show All Posts | Quote Reply
zimcoder
Level: VB Lord


Registered: 27-10-2003
Posts: 225
icon Re: how to create and bind datagrid writing code in asp.net

It is very easy to create a datagrid from code with many of the features you ask for. Lets get into the code

declare your datagrid as server control

<asp: DataGrid id="dg" runat="server" Width="459px" Height="198px" AutoGenerateColumns="False"AllowSorting="True" AllowPaging="True"></asp: DataGrid>


This declaration covers quite a bit. It declares a datagrid and sets its id to dg and indicates this is a server side control by specifying the runat as server. In this declaration we have three other important attributes set. The first is the AutoGenerateColumns="False" this allows the developer to specify which columns to bind to the datagrid rather than have all the columns in the datasource shown. The Allowsorting attribute is then set to true so that the user can sort the datagrid based on a column they choose. Allowpaging attribute is set to true so that paging capabilities can be added to the datagrid.

Let us now create the datasource.. i will use a datatable here:

DataTable dt = new DataTable();

//Add some columns
dt.Columns.Add("item",typeof (String));
dt.Columns.Add("price",typeof(float));

Datarow dr = new DataRow();

dr["item"]="pen";
dr["price"]=3.5;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["item"]="camera";
dr["price"]=10.8;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["item"]="coffee maker";
dr["price"]=40.2;
dt.Rows.Add(dr);


//select view and bind data
dg.DataSource=dt.DefaultView;
dg.DataBind();
We have dynamically created a datagrid and bound data to it in code

to remove a column from the datagrid we use
dg.Columns.Remove("item");






[Edited by zimcoder on 19-07-2004 at 04:53 PM GMT]

[Edited by zimcoder on 02-08-2004 at 08:15 PM GMT]

____________________________
BrainBench ADO.NET and ASP.NET Certified Developer

19-07-2004 at 02:52 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : ASP.Net : how to create and bind datagrid writing code in asp.net
Previous Topic (Problem displaying images from  database)Next Topic (why web user control can not run independly ?) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder