|
|
GridView With ObjectDataSource
AzamSharp
Published Date: 1/11/2006 6:38:36 PM
Views: 111282
Abstract:
In this article I will show you how you can use the ObjectDataSource with the GridView control to do editing, updating, deleting and adding new records. There are several ways to perform these operations, I am using the simplest approach. The project files are also attached with this article so please feel free to download them.
Introduction:
In this article I will show you
how you can use the ObjectDataSource with the GridView control to do
editing, updating, deleting and adding new records. There are several ways to
perform these operations, I am using the simplest approach. The project files
are also attached with this article so please feel free to download them.
| This application was
implemented in Visual Studio.NET 2005 Professional. |
Creating the User Interface:
Let's first see the user interface
that we are going to be using in this article.

As you can see in the image above our GridView
contains five columns. UserID, FirstName and LastName are template columns and
Edit and Delete columns are Command Columns which are added using smart tag
option (You can view the smart tag if you right click on the GridView control).
In the footer I have added TextBoxes for the
FirstName and LastName as I will be inserting new records. The Add button simply
adds the new records to the database.
Below is the complete HTML code of the GridView
control:
|
<asp:GridView
ID="GridView1"
runat="server"
AutoGenerateColumns="False"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing"
DataKeyNames="ID"
OnRowUpdating="GridView1_RowUpdating"
OnRowUpdated="GridView1_RowUpdated"
OnRowDeleting="GridView1_RowDeleting"
ShowFooter="True"
OnRowCommand="GridView1_RowCommand"
DataSourceID="objUser">
<Columns>
<asp:TemplateField
HeaderText="UserID">
<ItemTemplate>
<asp:Label
ID="lblUserID"
runat="server"
Text='<%#
Bind("ID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Button
ID="Btn_Add"
runat="server"
CommandName="AddUser"
Text="Add"
/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField
HeaderText="FirstName">
<ItemTemplate>
<asp:Label
ID="lblFirstName"
Text='<%#
Eval("FirstName") %>'
runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox
ID="txtFirstName"
Text='<%#
Eval("FirstName") %>'
runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox
ID="txtAddFirstName"
runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField
HeaderText="LastName">
<ItemTemplate>
<asp:Label
ID="lblLastName"
Text='<%#
Eval("LastName") %>'
runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox
ID="txtLastName"
Text='<%#
Eval("LastName") %>'
runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox
ID="txtAddLastName"
runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField
HeaderText="Edit"
ShowEditButton="True"
/>
<asp:CommandField
HeaderText="Delete"
ShowDeleteButton="True"
/>
</Columns>
</asp:GridView>
|
Creating a User Class:
Since, we are working with ObjectDataSource
control we need to make an entity class. Take a look at the simple User class
below:
|
public
class
User
{
/* PRIVATE
FIELDS */
private
int
_ID;
private
string
_firstName;
private
string
_lastName;
/* PUBLIC PROPERTIES */
public
int
ID
{
get
{ return
_ID; }
}
public
string
FirstName
{
get
{
if
(!String.IsNullOrEmpty(_firstName))
return
_firstName;
else
return
null;
}
set
{ _firstName = value;
}
}
public
string
LastName
{
get
{
if
(!String.IsNullOrEmpty(_lastName))
return
_lastName;
else
return
null;
}
set
{ _lastName = value;
}
}
|
This is just a small part of the class you can
view the whole class when you download the project files. The class contains
three properties which are ID, FirstName and LastName. We will be playing around
with these properties.
Creating the ObjectDataSource Control:
After setting up the GridView control on the
form the next task is to create an ObjectDataSource control. This is
pretty simply just drag and drop the ObjectDataSource control from the
toolbox on the form and that's it. After defining all the properties of the
ObjectDataSource control it looks something like this:
| <asp:ObjectDataSource
DeleteMethod="DeleteUser"
InsertMethod="AddUser"
ID="objUser"
runat="server"
SelectMethod="GetAllUsers"
UpdateMethod="UpdateUser"
TypeName="User">
</asp:ObjectDataSource> |
IMPLEMENTING A SELECT METHOD:
The first task is to display the data in the
GridView control. We need a method that can go to the database and fetch the
results. I have implemented this method in the User class. Take a look at the
code below:
|
public
List<User>
GetAllUsers() {
List<User>
userList = new
List<User>();
SqlConnection myConnection =
new
SqlConnection(ConnectionString);
SqlCommand
myCommand = new
SqlCommand(SP_GET_ALL_USERS,
myConnection);
myConnection.Open();
SqlDataReader
reader = myCommand.ExecuteReader();
while
(reader.Read())
{
User
user = new
User((int)reader["UserID"],(string)
reader["FirstName"],(string)
reader["LastName"]);
userList.Add(user);
}
myConnection.Close();
reader.Close();
myCommand.Dispose();
if
(userList != null
&& userList.Count > 0)
return
userList;
else
return
null;
}
|
As you can see it is a simple method that
returns the Generic List of type User. You have to set the ObjectDataSource
SelectMethod property to "GetAllUsers" and set the DataSourceID of
GridView to "objUser" (objUser is the name of the ObjectDataSource control).
Once, the GridView's DataSourceID is set to the ObjectDataSource control
it will automatically bind the ObjectDataSource to the GridView control.
By default the SelectMethod is bind to the GridView control.
IMPLEMENTING THE INSERT METHOD:
Now, let's see how we can implement the insert
functionality in the GridView. I have added TextBoxes in the Footer of the
GridView control which can also be added in the Header of the GridView control.
Let's see the AddUser method which is defined in the User class.
|
public
void AddUser(string
firstName, string
lastName) {
SqlConnection myConnection =
new
SqlConnection(ConnectionString);
SqlCommand
myCommand = new
SqlCommand(SP_ADD_USER,
myConnection);
myCommand.Parameters.AddWithValue("@FirstName",
firstName);
myCommand.Parameters.AddWithValue("@LastName",
lastName);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
|
The AddUser method simply accepts firstName and
lastName as a parameter and adds them to the database. SP_ADD_USER is a
simply string which contains the insert query you can download the complete code
and view it.
The "Add" button inside the GridView has the
CommandName property set to "AddUser". This way we can easily catch this
event inside the GridView_RowCommand event. Take a look at the code below
which catches the Add button click.
|
protected
void
GridView1_RowCommand(object
sender, GridViewCommandEventArgs
e) {
if
(e.CommandName == "AddUser")
{
string
firstName = ((TextBox)GridView1.FooterRow.FindControl("txtAddFirstName")).Text;
string
lastName = ((TextBox)GridView1.FooterRow.FindControl("txtAddLastName")).Text;
objUser.InsertParameters.Add("firstName",
firstName);
objUser.InsertParameters.Add("lastName",
lastName);
objUser.Insert();
}
} |
| The first parameter of the
objUser.InsertParameters.Add("thisone",firstName) has to be the exact same
in the AddUser method in the User class.
This also applies to
objUser.DeleteParameters and objUser.UpdateParameters. |
Once, the parameters reaches the AddUser method
in the User.cs class then it is a simple insert using the SqlCommand
object and running a simply INSERT query.
IMPLEMENTING THE DELETE METHOD:
I am sure you are with me so far. Okay on to
delete functionality, once again first we implement a method in the User.cs
class "DeleteUser". Here is the implementation of "DeleteUser"
method:
|
public
void DeleteUser(int
id) {
SqlConnection myConnection =
new
SqlConnection(ConnectionString);
SqlCommand
myCommand = new
SqlCommand(SP_DELETE_USER,
myConnection);
myCommand.Parameters.AddWithValue("@UserID",
id);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
} |
As you can see it simply accepts the id as a
parameter and attaches to the command object and runs the appropriate delete
query. Now that you have made the "DeleteUser" method you can implement
the GridView_RowDeleting event which is fired when the row is being
deleted.
|
protected
void
GridView1_RowDeleting(object
sender, GridViewDeleteEventArgs
e) {
//int index =
e.RowIndex;
//int userID =
Convert.ToInt32(GridView1.Rows[index].Cells[0].Text);
//objUser.DeleteParameters.Add("userID",TypeCode.Int32,userID.ToString());
int
index = e.RowIndex;
int
userID = Convert.ToInt32(((Label)GridView1.Rows[index].FindControl("lblUserID")).Text);
objUser.DeleteParameters.Add("id",TypeCode.Int32,userID.ToString());
objUser.Delete();
//BindData();
}
|
First I get the index of the row that is being
clicked using the e.RowIndex property. After I get the index I
simply use the FindControl method to locate the userID. lblUserID is the name of
the Label which displays the UserID. Once again remember that the parameters
"id" should be the exact same as you have defined in the DeleteUser
method in User.cs class.
| You must be wondering that
why have I placed the commented out code in the above code snippet. That
code can be used if you are using BoundColumns. I don't think it is a good
idea to use the Bound Columns when you plan to extract values from those
columns, the reason being that BoundColumns depends on the index position as
you can see Cells[0],Col[1] and so on. If later that index changes then you
need to go to your code and change it manually. |
IMPLEMENTING THE UPDATE METHOD:
Finally, we are to the last method which is
update. To be really honest this took me some time since I was trying to use the
e.NewValues and it was keep returning me nulls. Anyway, here is a small and
simply solution but I am sure there are many better ways to do the same task.
First let's take a look at the UpdateUser
method in the User.cs class.
|
public
void UpdateUser(int
id, string
firstName, string
lastName) {
SqlConnection myConnection =
new
SqlConnection(ConnectionString);
SqlCommand
myCommand = new
SqlCommand(SP_UPDATE_USER,
myConnection);
myCommand.Parameters.AddWithValue("@FirstName",
firstName);
myCommand.Parameters.AddWithValue("@LastName",
lastName);
myCommand.Parameters.AddWithValue("@UserID",
id);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
} |
I don't think I have to explain the above
method. It simply takes id, firstName, lastName and updates the record. Now
let's take a look at the GridView_RowUpdating event which is fired when
the row is being updated.
|
protected
void
GridView1_RowUpdating(object
sender, GridViewUpdateEventArgs
e) {
int
userID = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblUserID")).Text);
string
firstName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFirstName")).Text;
string
lastName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtLastName")).Text;
objUser.UpdateParameters.Add("id",
TypeCode.Int32,
userID.ToString());
objUser.UpdateParameters.Add("firstName",
firstName);
objUser.UpdateParameters.Add("lastName",
lastName);
objUser.Update();
GridView1.EditIndex = -1;
} |
I am using the same approach that I used for
"Delete" and "Add" functionality. In short I am using the rowIndex to find the
row that the user has clicked and then using FindControl method I find the new
value from the TextBox control. And finally send the new values to the
UpdateUser method which is defined in User.cs file.
Although I have demonstrated a very simple way
of update, delete and add functionality I am sure there exist a much better way
of doing the same tasks more efficiently.
I have attached the source code files with this
project so please free to download them.
I hope you liked this article, happy coding!
| If you are one of the
thousands that visit GridViewGuy for your .NET articles and resources, you
might be interested in making a donation. Extra cash helps pay for the
hosting services and speed things up
around here, and makes this website possible.
Make a Donation
Once, again thank you very much and
remember its because of you FINE people that this website
is up and running. |
|
Export Button is a custom control that let's you export your
DataGrid or TextBox data to several different formats. The
control is extremely easy to use and also exposes design time features. In
this article I will discuss some of the features of the Export Button and
how it benefits the developer.
BUY IT
NOW |
Enter Comment/Feedback
Comments/Feedbacks
|
|
|
|
Subject: Gridview and Objectdatasource
Name: Yusuf
Date: 1/27/2007 8:24:11 PM
Comment: Hi
I added a dataset using the dataset configuration wizard. Due to the complex nature of my sql query, the wizard did not generate the update command. Hence I wrote the updatecommand in the properties window of the tableadapter.
Then I configured an ObjectDataSource and added a gridview control. To the gridview control I added a TemplateField which has a dropdownlist in EditItemTemplate. The DataSource property of the dropdownlist is assigned to a method that returns a dataset (so as to display a list of possible values). Now, I am able to edit the gridview, and select the appropriate value from the dropdownlist. No problems here. But when I click Update in the GridView, I get the following message,
"ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'Update' that has parameters: Status, WorkHrs, Remarks, original_TaskID. "
Although I have defined the Update command and parameters of the dataadapter, asp.net is not finding them. I dont know where else I should specify the Update method. Could you please help me.
Best Regards |
|
|
|
|
|
Subject: good code
Name: srikanth
Date: 2/1/2007 3:07:21 AM
Comment: its very nice |
|
|
|
|
|
Subject: @ Srikanth
Name: Koushik
Date: 2/9/2007 1:27:43 AM
Comment: Hi Srikant
Here is the trick. Change the "OldValuesParameterFormatString="original_{0}" property of the "ObjectdataSource" to "OldValuesParameterFormatString="{0}".
And you are done.....
Cheers...
Koushik |
|
|
|
|
|
Subject: Thanks for the article
Name: Dave
Date: 2/9/2007 8:39:51 AM
Comment: Really helped me a lot! I was stuck on getting this working and wasted hours. I wish I'd found your website earlier.
I had the same problem with e.NewValues returning nothing.
Thanks again! |
|
|
|
|
|
Subject: hi
Name: rajasekaran
Date: 2/12/2007 1:41:01 AM
Comment: the above is very useful for me. |
|
|
|
|
|
Subject: Very good
Name: yogesh
Date: 2/13/2007 12:43:14 PM
Comment: Yes, it help me in main point. it saves my time. |
|
|
|
|
|
Subject: Thanks!
Name: Jay
Date: 2/15/2007 6:55:02 PM
Comment: thanks Azam. This was exactly the solution i was looking for.
God Bless. |
|
|
|
|
|
Subject: query
Name: Ni
Date: 3/17/2007 5:42:38 AM
Comment: Hi
Good Website .
However the grid displays the all the rows from the table , How can i filter it based on some Criteria, before loading the grid |
|
|
|
|
|
Subject: Error
Name: Kamal
Date: 4/1/2007 1:53:52 AM
Comment: Hi
When i am using the below code it throws an error like 'TextBox' does not contain a definition for 'Text'.please help me to solve this problem
string firstName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFirstName")).Text;
string lastName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtLastName")).Text;
Thanks
Kamal
|
|
|
|
|
|
Subject: RE: Error
Name: AzamSharp
Date: 4/25/2007 8:43:00 AM
Comment: Hi Kamal,
I don't see any problems in your statement. Try to retrieve the TextBox and check if it exists or not. |
|
|
|
|
|
Subject: Regarding this tag
Name: Gokul
Date: 5/14/2007 4:37:24 AM
Comment: the suggestion u have given is very useful. very thanks |
|
|
|
|
|
Subject: GridView With ObjectDataSource
Name: yashavant
Date: 5/24/2007 12:15:56 AM
Comment: Hi ,
I have gone through ur code. It is Quite simple and nice.
Only bug i found is that delete method get called twice when u delete any row.
Anyways nice post and let me know if u have any solution for same.
|
|
|
|
|
|
Subject: Improvement
Name: tipu
Date: 6/19/2007 2:57:18 AM
Comment: Sir,
I want to suggest that the beginners videos like how to use java script and Asp.net should be free to download.you will get more visits from developers and your site will become more popular.And you will increase your revenue from by getting more advertisements.
regards,
tipu
A.Soft.Engg
systems limited. |
|
|
|
|
|
Subject: Namespace
Name: Jereme Guenther
Date: 7/24/2007 1:50:39 PM
Comment: For those who are wondering, the namespace for the List<> is:
using System.Collections.Generic; |
|
|
|
|
|
Subject: Nice one
Name: Vjiay
Date: 8/18/2007 4:42:43 PM
Comment: Hi Koushik,
How did u figure this out. It does work. I spent hours and hours as everyone else. nice one.
Vijay |
|
|
|
|
|
Subject: QUERY
Name: Krunal
Date: 8/21/2007 10:16:59 PM
Comment: hi
this solution is very nice.
but i have one problem...
in my case i have empty table and i m not able to view the add row .
bcoz its in grid view and it is a footer.
if there is empty table then it should also display the add row to add new entries.
thanks.
Krunal |
|
|
|
|
|
Subject: Gridview and Objectdatasource
Name: Cass
Date: 8/22/2007 9:30:58 AM
Comment: Can anyone tell me what this
public List GetAllUsers()
{
List userList = new List();
and this is in vb.net
{
User user = new User((int)reader["UserID"],(string) reader["FirstName"],(string) reader["LastName"]);
userList.Add(user);
} |
|
|
|
|
|
Subject: vb equivalent
Name: Akash
Date: 8/30/2007 12:43:33 PM
Comment: That looks like a great idea..but i have
no idea abt generic class(:)), can u explain me a bit more abt that. Also how to create such a Generic List of type User in VB?
plz..help!! |
|
|
|
|
|
Subject: RE: VB.NET (Akash)
Name: AzamSharp
Date: 9/1/2007 11:26:30 AM
Comment: Hi Akash,
You can do something like this:
Dim users As New List(Of User)() |
|
|
|
|
|
Subject: Static Methods
Name: Shail
Date: 9/4/2007 7:52:52 AM
Comment: Hello Azam,
is it possible to have static versions of Add update and delete methods.
Like
public static int Add( list of parameters )
{
}
Thank you |
|
|
|
|
|
Subject: RE: Static Methods
Name: AzamSharp
Date: 9/5/2007 7:57:30 AM
Comment: Hi Shail,
Although I have not tried it. But I am sure that you can have static methods instead of instance methods. |
|
|
|
|
|
Subject: good
Name: vidhya
Date: 9/6/2007 9:29:26 PM
Comment: very helpfull.. and also very clear |
|
|
|
|
|
Subject: GridView with objectDatasource
Name: Alex Koranteng
Date: 9/6/2007 11:46:25 PM
Comment: I am getting the following error for your article on Gridview with objectdatasource. Any suggestions.
I created table using sqlserver2000
and named the fields, _ID, _firstName, and _lastName
Exception Details: System.IndexOutOfRangeException: UserID
Source Error:
Line 126: while (reader.Read())
Line 127: {
Line 128: User user = new User((int)reader["UserID"],(string) reader["FirstName"],(string) reader["LastName"]);
Line 129: userList.Add(user);
Line 130: }
|
|
|
|
|
|
Subject: RE: GridView with objectDatasource
Name: AzamSharp
Date: 9/9/2007 9:27:19 AM
Comment: Hi Alex Koranteng,
It seems that there is not column "UserID". Make sure that the database columns are named "UserID", "FirstName" and "LastName". |
|
|
|
|
|
Subject: No binding on Footer after deleteing last data row
Name: Dan
Date: 9/20/2007 5:06:42 PM
Comment: I'm having an issue where the controls in the footer row do not get bound after you delete the last record in the data source. Any one have any ideas why this is happening? |
|
|
|
|
|
Subject: ObjectDataSource and 2 other issues
Name: Mujeeb
Date: 9/21/2007 10:55:06 AM
Comment: Hi Azam,
Thanks for your code it really helped. I have 3 issues.
1) I am getting error On Update
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'UpdateDrivers' that has parameters: pkDriverID, LName.
Source Error:
2) Page won't get refereshed after i "Add New" record. But, when i click "Refresh" "OnRowCommand_GridView1" event fires.
3) same user id i am trying to use,
int index = e.RowIndex;
int userID = Convert.ToInt32(((HiddenField)GridView1.Rows[index].FindControl("lblUserID")).Text);
this is the error i am getting
{"Object reference not set to an instance of an object."}
Please help me out, i am stuck with only these 3 issues. You can email me @ drbin02@yahoo.com |
|
|
|
|
|
Subject: Grid View and Fotter Template
Name: teju
Date: 9/25/2007 1:28:22 AM
Comment: Hello,
I have used your code and seems to work good. Is there any way where I can display insert text boxes on the top (as the first record). So it is easier to insert new records when there are more records.
Hope I am being clear.
Regards,
Teju |
|
|
|
|
|
Subject: gridtemplate
Name: Richard
Date: 10/3/2007 2:23:41 PM
Comment: This works fine if right out of the box but if I have GridView with textbox template rows that are initially loaded with no values meaning null I can't get the edited value out and sent to my row updated function to then be updated in the database. I just keep getting null values. |
|
|
|
|
|
Subject: RE: gridtemplate
Name: AzamSharp
Date: 10/7/2007 9:15:35 AM
Comment: Hi Richard,
Make sure your TextBoxes are placed inside the GridView's EditItemTemplate column. |
|
|
|
|
|
Subject: Gridview Inserting
Name: Mark Henke
Date: 10/26/2007 7:13:17 AM
Comment: Try Inserting if there are no initial databound records! |
|
|
|
|
|
Subject: question
Name: partha
Date: 10/26/2007 11:20:49 AM
Comment: How do we run the code? There is no sln or project file. |
|
|
|
|
|
Subject: Grid view
Name: vinod
Date: 10/29/2007 12:07:06 AM
Comment: It is vey nice site for dot net developers,
I M enjoying and lerning from this site much.
Thanks |
|
|
|
|
|
Subject: You can help me -thanks
Name: Peter
Date: 11/8/2007 11:40:22 PM
Comment: I have a gridview and i want when i click
LinkButton Edit then field sign a textbox
and i can edit field of row in gridview on textbox !! i make tranfer field on textbox and can update but i can't make button Cancel !! If i don't want update |
|
|
|
|
|
Subject: New textbox value
Name: Kristof
Date: 11/12/2007 2:49:13 PM
Comment: Hi,
First of all, congrats on the article, it is more than usefull!
I have a slight problem though... When i try to retrieve the new value of one of the textboxes, as described above, i always get the original value...
I have tried many workarounds, but didn't manage to get it working yet. Is there a magical solution here?
Greetz |
|
|
|
|
|
Subject: RE: New TextBox Value
Name: AzamSharp
Date: 11/13/2007 8:50:14 AM
Comment: Hi Kristof,
Can you paste the code! |
|
|
|
|
|
Subject: awesome article
Name: somebody
Date: 11/27/2007 6:02:13 AM
Comment: awesome !
bunch of errors...
check many times before giving some advices |
|
|
|
|
|
Subject: vb.net version ObjectDataSource 'objUser' could not find a non-generic method
Name: Howie
Date: 12/8/2007 1:30:42 PM
Comment:
I was going absolutely insane trying to implement this as vb.net instead of cs.
I reduced the solution to a simple update but kept getting this error only in the VB version.
ObjectDataSource 'objUser' could not find a non-generic method 'UpdateUser' that has parameters: id, firstname, lastname, id1, firstname1, lastname1.
It was like it was adding 3 extra parameters (id1, firstname1, lastname1)....
After much research I realized the control was internally adding its own update clause seemingly just in the vb version becase the cs version.
One magic statement fixed the problem....
In the GridView1.RowUpdating event I added the following line BEFORE adding parameters.... Voila !!!
objUser.UpdateParameters.Clear()
I sincerely hope this helps someone else because it drove me crazy for a couple of days. |
|
|
|
|
|
Subject: RE: VB.NET Version ObjectDataSource
Name: AzamSharp
Date: 12/12/2007 1:00:22 PM
Comment: Hi Howie,
I am glad you solved the problem. And thanks for providing the solution to your problem. |
|
|
|
|
|
Subject: Footer row display when there are no rows
Name: manor
Date: 12/29/2007 6:27:19 AM
Comment: Hi,
This is a nice article. I am having one problem. I am populating the gridview based upon the date selected in the calendar control. When there are no records for that particular date, the gridview is not getting populated. I mean I am not getting footer row to add the new record. I am seeing that this is a gridview .net 2.0 behavior. How can resolve this issue?
Thanks,
Manor |
|
|
|
|
|
Subject: RE: Footer row displays when there are no rows
Name: AzamSharp
Date: 12/29/2007 6:28:45 PM
Comment: Hi Manor,
You can use the EmptyDataTemplate inside the GridView TemplateField columns which will be displayed when there is no data to display in the Gridview control. |
|
|
|
|
|
Subject: RE: Footer row displays when there are no rows
Name: manor
Date: 12/30/2007 6:40:41 AM
Comment: Thanks!
Yes...I am trying to use EmptyDataTemplate with DetailsView to show the empty record. But I was looking for a horizontal design (columnar format) for the empty fields. I would like to give users the same UI experience the way we get in gridview's footerrow. Is it possible to do it?
field1 field2 field2
as opposed to
field1
field2
field3
Thanks! |
|
|
|
|
|
Subject: Adapt for VS2008 ?
Name: Henrik Laurell
Date: 1/9/2008 6:49:52 AM
Comment: Perfect example, thanks!
Any idea on how to adapt this example for VS2008 ?
Each GridView method which calls to objUser.Insert, objUser.Update and objUser.Delete methods, fails in VS2008. It says there is no matching AddUser() etc methods with correct paramers. I guess there is something different in how connections are made between the Default.aspx.cs file and the User.cs class in latest version of Visual Studio.
Many thanks again for a great example. /Henrik
|
|
|
|
|
|
Subject: VS2008 problems
Name: Henrik Laurell
Date: 1/9/2008 8:37:29 AM
Comment: Hi again,
I was convinced it had something to do with my new version of Visual Studio, but below line solved it;
objUser.InsertParameters.Clear();
just before starting to add parameters. Same with Update/Delete.
Thanks again, this example has helped me alot.
/Henrik |
|
|
|
|
|
Subject: GridView and SqlDataSource
Name: Kuldeep
Date: 1/10/2008 1:02:48 AM
Comment: Hi,
I make all of these things properly through Sql DataSource by coding.
but when i clicked on the Edit Button there are no any textbox display. nothing will be change .
What's the Propb?
can any one tell me?
Best Regards |
|
|
|
|
|
Subject: VB
Name: Francisco
Date: 3/3/2008 7:57:20 AM
Comment: This is exactly what I need but in Visual Basic |
|
|
|
|
|
Subject: GridView
Name: RB
Date: 3/12/2008 12:06:46 PM
Comment: Very nice!
Could you please send me the source code/project?
Thank you,
RB |
|
|
|
|
|
Subject: RE: GridView
Name: AzamSharp
Date: 3/12/2008 8:29:48 PM
Comment: Hi RB,
You can download the project at the end of the article. |
|
|
|
|
|
Subject: GridView
Name: RB
Date: 3/16/2008 6:28:41 PM
Comment: Hi,
I downloaded the project and the project runs fine; I only had to create the table (Suggestion include the table in a text file in the project or as a attached database in the project)
The code is beautiful and nice!
Donation for sure!
Can we see the same project with LINQ soon? Please contact me and we can maybe do a deal…
Thank you very much for your good work and support!
I wish you the BEST!
Thank you,
RB |
|
|
|
|
|
Subject: Codeless Method
Name: johny
Date: 3/17/2008 4:49:37 AM
Comment: here's a bit of a challenge:
you've provided delete & update code, but the datasource control generates its own insert method, and gets all values from the databound control, automatically.
so, how can i bind those text boxes to data fields, so that i only need to call the insert method of the datasource, WITHOUT having to explictly perform InsertParameters calls on each field? |
|
|
|
|
|
Subject: MUY BUENO- Gridview
Name: JOSE ARTURO PITTI
Date: 3/25/2008 6:31:14 AM
Comment: Gracias por el ejemplo realmente sencillo, y esta es una de las cualidades de 2.0 manejas todo de una forma mas simple inclusive a traves de clases, como lo expones en tu ejemplo. Saludos |
|
|
|
|
|
Subject: FooterTemplate
Name: Medes
Date: 4/18/2008 8:00:31 AM
Comment: Hi and thanks for your good article. I have just a question when my gridview is empty the footerTemplate are invisible and I can not add new row. How can I fix it? thanks |
|
|
|
|
|
Subject: source code link
Name: Dos
Date: 4/30/2008 4:40:53 AM
Comment: Thanks for this! Just a webs design tip though, the download source code is almost impossible to see. Dark yellow/ light brown on a dark brown background?! |
|
|
|
|
|
Subject: RE: Source code link
Name: AzamSharp
Date: 4/30/2008 9:22:06 AM
Comment: Hi Dos,
Thanks for the tip! You will notice that in the newer articles I am using the default link which is more visible. |
|
|
|
|
|
Subject: good code but problem in vb conversion
Name: raj
Date: 5/1/2008 2:22:39 AM
Comment: this is really a very nice article, but when i m using vb file instead of cs file , it is inserting records twice..it's calling rowcommand and other events for updating and deleting twice..pls help me why this is happing..i have also added objuser.updateparameter.clear() berore adding parameter same for delete and insert also..but it's still not working..
Thanks,
raj |
|
|
|
|
|
Subject: inserting records twice
Name: moh1980
Date: 5/3/2008 9:46:06 PM
Comment: thank you very much for this nice artical
its happen to me when i use vb.net the inserting records fired twice does any sugest |
|
|
|
|
|
Subject: Really good object oriented Code
Name: Rohan
Date: 5/15/2008 4:27:23 PM
Comment: Kepp up the good work |
|
|
|
|
|
Subject: Grid view
Name: J.Rajasekhar
Date: 5/26/2008 4:15:31 AM
Comment: Excellent work done. It is not esay to build application with out some hard code but u did a very excellent job . what ever iam very happy about this article. |
|
|
|
|
|
Subject: Gridview editing
Name: Rajesh
Date: 5/30/2008 10:27:41 AM
Comment: Thank you very much for this article. |
|
|
|
|
|
Subject: Rowcommand
Name: Andrew
Date: 8/21/2008 12:49:49 PM
Comment: I wonder if I'm missing something. The 'Add' button doesn't seem to fire the rowcommand event in the gridview. Is there a workaround for this???? |
|
|
|
|
|
Subject: RE: RowCommand
Name: AzamSharp
Date: 8/25/2008 8:01:21 PM
Comment: Hi Andrew,
If the button is inside the GridView control then it should fire the RowCommand event.
Make sure you have placed the button inside the GridView control. |
|
|
|
|
|
Subject: query
Name: rudiger
Date: 9/28/2008 10:21:55 PM
Comment: If the database table we are selecting is empty, then it must allow us to show a gridview with empty cells with atleast one row.... how to do that???
|
|
|
|
|
|
Subject: won't update if field is null
Name: Jeremy
Date: 10/16/2008 2:07:14 PM
Comment: This is a great article and provided exactly what I was looking for. The only issue I have run across, is if one of the fields contains an empty sting or null value, the update fails (i.e. address2 field is empty in an address). Do you have any suggestions for that? Thanks. |
|
|
|
|
|
Subject: dfhdhfdgh
Name: dfgdghf
Date: 4/3/2009 5:15:57 AM
Comment: You must be wondering that why have I placed the commented out code in the above code snippet. That code can be used if you are using BoundColumns. I don't think it is a good idea to use the Bound Columns when you plan to extract values from those columns, the reason being that BoundColumns depends on the index position as you can see Cells[0],Col[1] and so on. If later that index changes then you need to go to your code and change it manually. |
|
|
|
|
|
Subject: Nested gridview edit not working
Name: Carol
Date: 10/19/2009 8:08:21 AM
Comment: Thanks for a great article it was very helpful. I converted it to VB. However, my gridview is in a template field within another gridview. When the user clicks on a button in the parent gridview, I databind the childgridview. I can view the data OK but when I click edit I get "object not set to an instance of an object". e.NewEditIndex = -1. Any idea how to get the index of the current row?
Thanks |
|
|
|
|
|
Subject: doubt
Name: jayaraj
Date: 2/2/2010 1:58:34 AM
Comment: the above code is excellent.
If i add a column like image means,how to
edit and update image....please help me..
thanks
|
|
|
|
|
|
Subject: BoundColumns
Name: Nick
Date: 2/17/2010 3:36:37 PM
Comment: You can use the Header collection to find the column number to use...send me an email if you want the exact code...its at my house. BoundColumns is so much cleaner. :) |
|
|
|