Solved

How to build a GridView dynamically?

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

19 Experts available now in Live!

Get 1:1 Help Now