473,388 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

A better way to add a row to a gridview

After spending two days looking at the options in adding a row to a
gridview, I came up with my own that I'd like to share. I only can
share pseudo-code right now, but maybe in the future I'll write a full
fledged article.

I didn't like the options, such as putting your controls (really,
DUPLICATING your controls) in the Footer for a variety of reasons.
First, I don't like the idea of having to do things twice, including
any validation. 2nd, I really want to add a row to the TOP so the
user doesn't have to scroll down. So what to do?

Here's the idea:

--There is a separate button for the "Add New Row" function.

--The button will programmatically add a row to the top of the data
source of the GridView, rebind the control, place that row into edit
mode, and pre-populate any required fields. The primary key should be
set to a value that wouldn't ordinarily be used to identify this row
as "new". In my case, I set the PK to -1

--In the update command (there is no separate Insert command), we
check to see if the PK is -1. If so, we do an "insert into" else we
do an "update table..."

--The rest of the function of the GridView works the same, and
everything else is pretty much automatically handled.

----------------
I created a Business Object for my datasource, with an Update, Select,
and Delete command.

My SelectCommand gets passed a value, either 0 or 1. If 1, we're
going to add a blank row to the top of the datatable...

Shared Function SelectCommand(ByVal blankrow As Integer) As DataTable
Dim MyDataTable As New DataTable()
Dim MyConnection As New
SqlConnection(ConfigurationManager.ConnectionStrin gs("CS").ConnectionString)
Dim MyCommand As New SqlCommand()
Dim MyReader As SqlDataReader
Dim ms As String

ms = "SELECT ...... " ' put your select statement here with
parameters

MyCommand.CommandText = ms
MyCommand.Connection = MyConnection
MyCommand.Connection.Open()

MyReader =
MyCommand.ExecuteReader(CommandBehavior.CloseConne ction)
MyDataTable.Load(MyReader)

If blankrow Then
Dim myRow As DataRow
myRow = MyDataTable.NewRow

myRow.Item("field1") = ""
myRow.Item("membership_id") = -1 'this is my primary key
myRow.Item("field2") = 0

MyDataTable.Rows.InsertAt(myRow, 0)
End If

MyCommand.Dispose()
MyConnection.Dispose()

Return MyDataTable
End Function
--------------
Now, when a user click on the Add New Row button, we do the
following....

GridView1.Columns(0).Visible = False 'I like to hide the
delete column when in Edit Mode
GridView1.DataSource = SelectCommand(1) ' we reselect the
Selectcommand, passing a 1
GridView1.DataSourceID = Nothing
GridView1.DataBind()
GridView1.EditIndex = 0 'and we're going to edit the first
row
--------------
So the user click the Add New Row button, a blank row is added to the
top of the GridView, and is placed into Edit mode.

In the UpdateCommand, we do the following:

Dim mySql As String

Dim MyConnection As New
SqlConnection(ConfigurationManager.ConnectionStrin gs("cs").ConnectionString)
Dim MyCommand As New SqlCommand()

If membership_id = -1 Then
mySql = "Insert into table... set field1=@field1 "
Else
mySql = "Update table set field1=@field1 where pk=@pk"
End If

MyCommand.CommandText = mySql
MyCommand.Connection = MyConnection

With MyCommand.Parameters
.Add("@param1", SqlDbType.Int).Value = field1
'add all parameters
End With
Dim result As Integer = 0

Try
MyConnection.Open()
result = MyCommand.ExecuteNonQuery()
Catch ex As Exception
Throw ex
Finally
MyConnection.Close()

End Try

Return result

May 1 '07 #1
0 6483

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: theKirk | last post by:
using Visual Studio 2005 C# ASP.NET I know there has to be a simple way to do this....I want to use C# in a code behind for aspx. Populate a GridView from an xml file Add Fields to the...
6
by: Nalaka | last post by:
Hi, I have a gridView (grid1), which as a templateColumn. In the template column, I have put in a gridView (grid2) and a ObjectDataSource (objectDataSource2). Question is... How to I pass the...
3
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....
5
by: sutphinwb | last post by:
Hi - This could be a simple question. When I relate two tables in a datasetet, how do I get that relation to show up in a GridView? The only way I've done it, is to create a separate table in the...
1
by: Roy | last post by:
Hey all. Below is the nested syntax on how to make a "codeless" nested gridview embedded within another gridviews templatefield column. Only problem is that it loads slow. REAL SLOW. There has to...
2
by: kiran kumar | last post by:
Task is there are 200 controls (TxtBoxes,ddlists) which is better Grid r normal table. this table/grid i have to use it for view/update the 2 tables data in database.How can i solve this. ...
2
by: antonyliu2002 | last post by:
I've been googling for some time, and could not find the solution to this problem. I am testing the paging feature of gridview. I have a very simple web form on which the user can select a few...
6
by: RobertTheProgrammer | last post by:
Hi folks, Here's a weird problem... I have a nested GridView setup (i.e. a GridView within a GridView), and within the nested GridView I have a DropDownList item which has the...
3
by: Peter | last post by:
I have a GridView which is populated by List<ofObjects> Does anyone have example of how to sort the columns of this GridView? I have found examples without DataSourceControl but these use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...

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.