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()
-
{
-
GridView1.Columns.Clear();
-
-
DataTable dataTable = Book.GetBooksDataSet().Tables[0];
-
-
CommandField cf = new CommandField();
-
cf.ShowEditButton = true;
-
-
GridView1.Columns.Add(cf);
-
-
int colCount = 1;
-
foreach (DataColumn c in dataTable.Columns)
-
{
-
BoundField boundField = new BoundField();
-
-
boundField.DataField = c.ColumnName;
-
boundField.HeaderText = c.ColumnName;
-
//boundField.FooterText = "---";
-
-
if (colCount == 3 || colCount == 5)
-
{
-
boundField.ReadOnly = true;
-
}
-
-
GridView1.Columns.Add(boundField);
-
colCount++;
-
}
-
-
GridView1.ShowFooter = true;
-
-
GridView1.DataSource = dataTable;
-
GridView1.DataBind();
-
-
GridViewRow footerRow = GridView1.FooterRow;
-
Button b = new Button();
-
b.Text = "Add New";
-
int i = 0;
-
footerRow.Cells[i].Controls.Add(b);
-
foreach (DataColumn c in dataTable.Columns)
-
{
-
++i;
-
TextBox tb = new TextBox();
-
footerRow.Cells[i].Controls.Add(tb);
-
}
-
}
-
....................................
-
....................................
-
....................................
-
}
-
But the problem is, when I click the "Add New" - button, it disappears instantly. And, also I am unable to add any event handler to it. Or intercept its actions like this: - protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
-
{
-
int index = Convert.ToInt32(e.CommandArgument);
-
-
if (e.CommandName == "Edit")
-
{
-
GridView1.EditIndex = index;
-
-
GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
-
-
//We can get cell data like this
-
string id = selectedRow.Cells[1].Text;
-
string isbn = selectedRow.Cells[2].Text;
-
-
//This is necessary to GridView to be showed up.
-
CreateGridView();
-
}
-
else if (e.CommandName == "Update")
-
{
-
LinkButton updateButton = (LinkButton)e.CommandSource;
-
-
DataControlFieldCell dcfc = (DataControlFieldCell)updateButton.Parent;
-
-
GridViewRow gvr = (GridViewRow)dcfc.Parent;
-
-
//The update...................
-
//Update grid-data to database
-
UpdateDataInTheDatabase(gvr.Cells[1].Controls);
-
-
//Grid goes back to normal
-
GridView1.EditIndex = -1;
-
-
//This is necessary to GridView to be showed up.
-
CreateGridView();
-
}
-
}
One more thing, I have seen some solutions that suggests to handle the GridView's `rowBound` event. But I need to do it from within `Page_load` event handler, or in, `GridView1_RowC ommand` event handler.
0 2704 Sign in to post your reply or Sign up for a free account.
Similar topics |
by: NateDawg |
last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if
anyone has some input it would sure help me out.
Ok, I’ve noticed a few gridview problems floating around the forum. Everyone
wants to do a java confirmation box when a user clicks the delete button.
Fair enough, basic user design rules state that you should always confirm a
delete action. There is also a consensus that the best way to do this is a
template...
|
by: Miguel Dias Moura |
last post by:
Hello,
I have a GridView in my page which is created in runtime. It works fine.
My page has 2 Asp Buttons:
- The HIDE button makes GridView.Visible = False;
- The SHOW button makes GridView.Visible = True.
I press HIDE and the GridView disappears as expected.
After it I press SHOW and the GridView doesn't show.
|
by: Flinky Wisty Pomm |
last post by:
Hi all, I've got a really annoying problem that I need to fix sharpish.
I've got a GridView derived control which has a templated header and
footer. It works wonderfully on the first render but then the
header/footer vanish into thin air. If I add the header/footer
onDataBound then they disappear when I do a postback with no
databinding. If I add them during PreRender, then they persist, but one
of my data rows is emptied for each time I...
|
by: Stu Lock |
last post by:
Hi,
I've spent the last hour trawling google for this - but all I find are
people asking the same question!
I have a gridview which is being databound to an empty datasource. I can
display the 'EmptyDataText' no problem, but the header and footer do not
display. The header I can live without - but the footer contains the 'Add
new item' form elements, so needs to be visible when the grid is empty.
|
by: yossimotro |
last post by:
Hi,
I'm using a gridview in my project and its footer to add new records.
The gridview is connected to an AccessDataSource which contains select,
insert and delete commands.
The problem occurs when the AccessDataSource is empty, the gridview
won't show at all, not even the footer so I can't add new data.
I know I can give up on the footer and just create somelike myself
| |
by: AG |
last post by:
Below is code (slightly modified and converted to VB) that was provided to
me in response to another post. I am using it to demonstrate another
problem.
In order for paging and other features to work properly in a gridview,
viewstate must be enabled. So, in order to minimize the size of viewstate
for a page, I will sometimes turn off viewstate for each control in template
columns.
That means that the gridview must be databound on each...
|
by: =?Utf-8?B?Vmlua2k=?= |
last post by:
Hello Everyone,
I have a gridview. I calculated some values in the gridview for the footer
of the gridview so basically I am adding all the values that are displayed in
the gridview for each column and displaying the total in the footer of the
gridview.
Now I export the gridview to excel spreadsheet with the code below.
Everything is exported well except the footer value become zero in the excel
spreadsheet. Can anyone tell me how can i...
|
by: gnewsgroup |
last post by:
Well, I am trying to use the footer row of a GridView for insertion
purpose. There are some articles about this, for example, the
gridviewguy.com has an example, which always displays the footer row.
I would like to display the footer row, only when a LinkButton "Add
New" is clicked. And after the new record is saved to the database,
let the footer row disappear.
In the LinkButton's click event handler, I simply say:
|
by: tina2626 |
last post by:
I m using this code in C#.net, for dynamic creation of GridView without using DB.
<CODE>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session = CreateDataTable();
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |