Solved

How to build a GridView dynamically?

Posted on 2007-12-01
4
936 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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