473,780 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Creation of GridView using DataTable

10 New Member
I m using this code in C#.net, for dynamic creation of GridView without using DB.

<CODE>


protected void Page_Load(objec t sender, EventArgs e)
{
if (!Page.IsPostBa ck)
{
Session["myDatatabl e"] = CreateDataTable ();

GridView1.DataS ource = ((DataTable)Ses sion["myDatatabl e"]).DefaultView;
GridView1.DataB ind();
}
}


private void myDataGrid_Dele teCommand(objec t source, System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
}
private DataTable CreateDataTable ()
{
DataTable myDataTable = new DataTable();

DataColumn myDataColumn;

myDataColumn = new DataColumn();
myDataColumn.Da taType = Type.GetType("S ystem.String");
myDataColumn.Co lumnName = "User Name";
myDataTable.Col umns.Add(myData Column);

myDataColumn = new DataColumn();
myDataColumn.Da taType = Type.GetType("S ystem.String");
myDataColumn.Co lumnName = "First Name";
myDataTable.Col umns.Add(myData Column);

return myDataTable;

}
private void AddDataToTable( string username, string firstname, string lastname, DataTable myTable)
{
DataRow row;

row = myTable.NewRow( );

row["User Name"] = username;
row["First Name"] = firstname;

myTable.Rows.Ad d(row);
}
protected void Button1_Click(o bject sender, EventArgs e)
{
if (TextBox1.Text. Trim() == "")
{
Label1.Text = "You must fill a username.";
return;
}
else
{
AddDataToTable( TextBox1.Text.T rim(), TextBox2.Text.T rim(),Label2.Te xt, (DataTable)Sess ion["myDatatabl e"]);

GridView1.DataS ource = ((DataTable)Ses sion["myDatatabl e"]).DefaultView;
GridView1.DataB ind();

TextBox2.Text = "";
TextBox1.Text = "";
Label1.Text = "";

}
}


</CODE>

Can any one tell how to delete data in gridview,.....

Thanks in Advance
With Regards
Tina
Apr 2 '08 #1
0 1664

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2386
by: K B | last post by:
PLEASE be patient and read this. I believe I've actually reached the end of the Web trying to find the answer! I have an aspx using a MasterPage with a ContentPlaceholder. I have a datatable that I use to dynamically populate a gridview - I have to do this in order to specify which control type to put in one of the columns. All this works fine. I've run Trace and can clearly see that the control.id I assigned (lblQuestionID) is there...
3
1979
by: Richard Carpenter | last post by:
I have a simple winform with a customers combobox, a "get orders" button and an orders grid. Due to other requirements, I have implemented the form to allow the user to select a customer from the combobox and click the "get orders" button to refresh the gridview with the selected customer's orders, via a GetDataByCustomerID stored procedure. The underlying Orders table includes, among others, three boolean columns and one particular...
4
8336
by: Ken Wigle | last post by:
All, I would be very grateful for any help on this question. I have an application in asp.net 2.0 where I dynamically create a datatable and then bind that to a gridview. Unfortunately, the date column always shows the date and time while I just want the short date. I have tried applying a format string {0:d} but to no avail. I saw a lot of posts regarding the htmlencode property but I do not know how to turn that off for a...
4
26707
by: Tomasz Jastrzebski | last post by:
Hello Everyone, I have a GridView control bound to a plain DataTable object. AutoGenerateEditButton is set to true, Edit button gets displayed, and RowEditing event fires as expected.
3
3696
by: Lloyd Sheen | last post by:
Is there an example of VB code which will take a datasource and extract from that the needed info (columns etc.) to create the columns and then bind the data? This seems so much more difficult to do an ad-hoc query. Lloyd Sheen
5
4681
by: =?Utf-8?B?QWRhciBXZXNsZXk=?= | last post by:
Hi All, I have a GridView inside the EditItemTemplate of a FormView. Both FormView and GridView are data bound using an ObjectDataSource. When the FormView's ObjectDataSource object has a SelectParameters with a SessionParameter in the parameters collection and the object stored in the Session is a reference type, the DataGrid fails to enter Line Editing mode. Is this a bug? Is there a workaround for this problem?
0
1266
by: Craig Buchanan | last post by:
I am adding templates to a gridview dynamically. these columns are based on data values that are generated prior to calling the gridviews databind. the challenge is when the form does a postback. i need to recreate these templates. until now, i've been storing the data needed to create the dynamic columns in a session variable. i was hoping to use the viewstate, but the viewstate isn't loaded until page_load. the gridview's templates...
0
1114
parshupooja
by: parshupooja | last post by:
hey all, I have DataTable binded to Gridview . This Datatable contains Data from Database. Before binding it to Gridview I am adding extra line with few controls dynamically in it such as text box below. When page loads I see extra Text box on the bottom of gridview but Validation is not working. Any Idea? I am calling this Function on each page load. Private Sub AddControls() Dim con As SqlConnection = New...
0
2705
by: edurazee | last post by:
I am able to create BoundFields and Footer-rows dynamically like this in my GridView: protected void Page_Load(object sender, EventArgs e) { CreateGridView(); } private void CreateGridView() {
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9931
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6727
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.