Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 942
  • Last Modified:

How to build a GridView dynamically?

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
pushkin
Asked:
pushkin
2 Solutions
 
Mortaza DoulatyCommented:
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
 
digitalZoCommented:
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
 
Computer101Commented:
Forced accept.

Computer101
Community Support Moderator
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now