Solved

How to build a GridView dynamically?

Posted on 2007-12-01
4
938 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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