-----------------------------------------------------------

New Message on MumbaiUserGroup

-----------------------------------------------------------
From: Swapnil_B1
Message 1 in Discussion

  Folks,                We have talked about GridView and its difference with 
DataGrid in my Previous article. This article covers a quick glimpse on how to 
create assign Datasource control to GridView, Editing, Paging, Deleting and 
sorting a Gridview without writing even a single line of code.  
In my previous article about the DataGrid, and how Paging and Sorting were what 
was called "built-in"? And then, when you found out what a hassle it was, even 
though it was a lot better than what you had before, you still were kind of 
disappointed?  
Well - now we have the GridView, in ASP.Net 2.0 and your disappointment is 
over! The 'AllowPaging' and 'AllowSorting' properties mean exactly what they 
say - once you set them to 'True' - that's it - that's all you need to do. It's 
what we were all wishing for with the DataGrid.  
But wait - there's more (I hope this doesn't sound too much like an 
infomercial)! With only one other control on the page, you can display data 
quickly and efficiently and never write a line of code to bind to the GridView. 
That control is the DataSource control. Add a DataSource control to the page, 
assign the desired values to the properties both it and the Gridview and you're 
through. No more forgetting the Sorting Sub or the Paging Sub or forgetting 
that you need a DataSet - not a DataReader, in able to add sorting and paging 
to the grid.  
I know - you might be saying to yourself "That can't be correct!", but I assure 
you, it is. Here's all you need to display data from your favorite Database 
table:  
<asp:GridView id="MyGridView" DataSourceID="MyDataSource1" 
Runat="Server"/><asp:SqlDataSource ID="MyDataSource1" runat="server"   
ConnectionString="Server=YourServer;uid=YourUID;pwd=YourPWD;database=YourDB"  
ProviderName="System.Data.SqlClient"  SelectCommand="SELECT [Field Name List] 
FROM [Table Name]"></asp:SqlDataSource> 
Really! - that's all there is to it! One thing to remember is that the Provider 
name is apparently case-sensitive, so make sure that's entered correctly. And - 
as was noted earlier, to enable Paging and/or Sorting, just add the 
'AllowPaging' and 'AllowSorting' properties to the GridView:<asp:GridView 
id="MyGridView"        DataSourceID="MyDataSource1"        AllowSorting="True"  
      AllowPaging="True"        Runat="Server"/> 
Naturally, there are many more features/attributes to the GridView, much like 
the DataGrid, including all the formatting options you might need. And, you're 
probably saying - "Yea - but what about updating/editing?". Well, that's gotten 
a lot easier also. Just a few things to add.  
1.      Enter an Updatecommand (Update SQL Statement) to the existing 
DataSource, just as you added a SelectCommand.  
2.      Add two properties to your GridView (AutoGenerateEditButton and 
DataKeysNames)  
So - here's what's needed in the changed Controls (using Pubs as an example):  
<asp:GridView id="MyGridView"         DataSourceID="MyDataSource1"         
AllowSorting="True"         AllowPaging="True"         DataKeys="au_id"        
AutoGenerateEditButton="True"        Runat="Server"/><asp:SqlDataSource 
ID="MyDataSource1" runat="server"   
ConnectionString="Server=YourServer;uid=YourUID;pwd=YourPWD;database=YourDB"  
ProviderName="System.Data.SqlClient"  SelectCommand="SELECT [au_id], 
[au_fname], [au_lname], [phone] FROM [Authors]"  
UpdateCommand="UpdateCommand="Update [pbAuthors] Set [EMAIL PROTECTED], [EMAIL 
PROTECTED], [EMAIL PROTECTED] WHERE [EMAIL PROTECTED]"></asp:SqlDataSource> 
Here, the 'Where clause' (WHERE [EMAIL PROTECTED]) gets it's record key from 
the key assigned in the DataKeys property in the GridView.  
One other way to limit viewed fields from the table is to use 'BoundFields', 
very similar to the way the DataGrid works. First, like the DataGrid, turn off 
the auto-generation of the columns, with the property:  
AutoGenerateColumns="False" 
Then, inside the Start and End tags for the GridView, insert a 'Columns' 
section, and list each of the columns you need to show, separately:<Columns>    
    <asp:BoundField HeaderText="ID" DataField="au_id" ReadOnly="true" />        
<asp:BoundField HeaderText="Last Name" DataField="au_lname" /></Columns>The 
'ReadOnly' attribute is used when you don't want that particular field to be 
edited. For each column you would like to use for sorting, also add a 
SortExpression attribute, most likely, the same as the DataField: 
<asp:BoundField HeaderText="Last Name" DataField="au_lname" 
SortExpression="au_lname" />  
And, lastly, to add Deleting capability, it's just as easy. All you need to do 
is to add a 'DeleteCommand' to the DataSource Control:DeleteCommand="Delete 
from [Authors] Where [EMAIL PROTECTED]" 
Next, set the 'AutoGenerateDeleteButton' to 'True' in the GridView , and then, 
as you did with the UpdateCommand, make sure the DataKeys Property is set to 
the Primary Key for your table.  
Swapnil (swaps)  
http://swapsnet.spaces.live.com/

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/MumbaiUserGroup/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member 
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you 
received this message by mistake, please click the "Remove" link below. On the 
pre-addressed e-mail message that opens, simply click "Send". Your e-mail 
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to