Solved

How to build a GridView dynamically?

Posted on 2007-12-01
4
935 Views
Last Modified: 2013-11-07
Hello everybody,
I'm fairly new to ASP.NET, so I'm turning to you for help.  Here's what I'm looking for.

I need to dynamically build a GridView on the page.  The database that I'm working with is Oracle10.  I need to pass the name of the Table in Oracle and the page would build a GridView control that would be populated with data from that table.  Every column should be sortable.  Every record would need to have the Delete option only.  When the user clicks on Delete link, a message box should come up with confirmation ("Are you sure you want to delete this record?").

Please provide either a code snippet on how it's done or point me to a source where I can find the code snippet.

This is very urgent and I appreciate any help you can provide.

Just to summarize.  I'm passing the name of the table and the GridView needs to be built with data and the Delete link next to each record.

Thanks.
0
Comment
Question by:pushkin
4 Comments
 
LVL 12

Assisted Solution

by:Mortaza Doulaty
Mortaza Doulaty earned 125 total points
ID: 20391649
This code may help:

(also have a look at: http://aspalliance.com/757_Code_Snip_Populating_GridView_Control_Using_ASPNET_and_C_with_MouseOver_Effect.2)
using System.Data;
using System.Data.OracleClient;
OracleConnection connection = new OracleConnection("MyConnectionString");
OracleCommand command = new OracleCommand();
OracleDataAdapter adapter = new OracleDataAdapter(command);
// With...
DataSet ds = new DataSet();
adapter.fill(ds);
connection.Close();
GridView1.DataSource = ds;
GridView1.DataBind();

Open in new window

0
 
LVL 10

Accepted Solution

by:
digitalZo earned 125 total points
ID: 20393911
First - For Sortable GridView1 - when you bind the code [mortaza has provided the code for it], use the DataView method and bind the data to the Cache too. Something like this:

ds = New DataSet()
adapter.Fill(ds, [TableName])

dv = New DataView(ds.Tables(TableName))
Cache(DataStore) = dv
GridView1.DataSource = dv
GridView1.DataBind()

Then call it in GridView_Sorting event:

Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting

dv= Cache(Data)
dv.Sort = e.SortExpression.ToString()
GridView1.DataSource = dv
GridView1.DataBind()
End Sub

============

Second- for Delete button in every record and confirmation on GridView1:

In the Source code where the GridView1 is defined:

<asp:TemplateField ShowHeader="False">
<ItemTemplate>
  <asp:LinkButton ID="linkBtn" runat="server" CausesValidation="False" CommandName="Delete"
                 OnClientClick='return confirm("Are you sure you want to delete this entry?");'
                 Text="Delete" />
</ItemTemplate>
</asp:TemplateField>

hth.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20953004
Forced accept.

Computer101
Community Support Moderator
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now