473,396 Members | 1,775 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,396 software developers and data experts.

c# DataGrid View

Hey All

i need a usefull link to help me update, delete, insert and select data using a datagrid view.

and if there is a way to put the delete and update button in the same row.

i already did some of that but i realy need the right way to do so.

im using c# with mysql but its ok if tutorail is wil SQL server
Mar 25 '09 #1
1 4835
There are two methods Using sql commands with text boxes and so but you have to do some coding to data ding these inset text boxes. So if you don’t like to do much coding here is a short cut.

The GridView control provides a template called Empty Data Template. This template is displayed when there is no data to be displayed in the GridView. Normally this template is used to display a status message to the end user that there is no data to be displayed. However, you can use this template for any other purpose also. In this example you will use it to add a new record to the GridView.

Creating a Sample Web Form
Begin by creating a new web site in Visual Studio. Drag and drop an SQL Data Source control and configure it to select CustomerID, CompanyName, ContactName and Country columns from Customers table of Northwind database.



Make sure to choose Advanced button and select "Generate INSERT, UPDATE and DELETE" statements checkbox.



Now add a GridView control to the web form and set its DataSourceID property to SqlDataSource1. Also enable editing, deleting and paging for the grid. From the smart tag of the GridView select "Edit columns..." option.



Add a ButtonField to the grid and set its CommandName property to Insert. Users will click on Insert button to insert a new record.




Now right click on the GridView and choose Edit Template - Empty Data Template menu option. Drag and drop a DetailsView control inside Empty Data Template and set its DataSourceID property to SqlDataSource1.



Also set its DefaultMode property to Insert. This way when the Empty Data Template is displayed the DetailsView will be ready for insertion.

Now go in the code behind of the web form and write RowCommand event handler of GridView as shown below:

protected void GridView1_RowCommand
(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
GridView1.DataSourceID = "";
GridView1.DataBind();
}
}Here we check if the CommandName of GridViewCommandEventArgs class is Insert. If so we set DataSourceID property of GridView to empty string and call DataBind() method of GridView. This way the grid will not have any data and Empty Data Template will be displayed.

Now handle ItemInserted event of DetailsView control. The ItemInserted event is raised when DetailsView inserts a new record successfully. Write the following code inside the ItemInserted event handler:

protected void DetailsView1_ItemInserted
(object sender, DetailsViewInsertedEventArgs e)
{
GridView1.DataSourceID = "SqlDataSource1";
GridView1.DataBind();
}Here we set DataSourceID property of GridView again to SqlDataSource1 and bind it again. This way the grid will reflect the newly inserted record.


hope this will help you.
Mar 27 '09 #2

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

Similar topics

8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
2
by: Chris Plowman | last post by:
Hi all, I was wondering if anyone can help me with a really annoying problem I have been having. I made a derived datagrid class that will select the row when a user clicks anywhere on a cell...
3
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found...
5
by: Jeff | last post by:
IDE: VS 2003 :NET OS: XP Pro My app have a form with a tab-control on it. The tab-control have 2 tabpages. One of the tabpages displays a datagrid, and the other tabpage displays details (order...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
4
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a...
4
by: Suzanne | last post by:
Hi all, I'm having problems with datagrids and the currentcellchanged event. My problem is this: I have a datagrid on a form, if the user changes the text in a cell on the datagrid then tries to...
2
by: simonyong | last post by:
hello, anyone.. im newbie to asp.net my problem is : My task is related to view all data from a table called "employee " i had create a datagrid where called data from database and...
3
by: simonyong | last post by:
Im a newbie to asp.net, i hav a bit dilemma when thinking about how i should let it work.. My task as below: My task is related to view all data from a table called "employee " i had create a...
2
by: slinky | last post by:
I had a successfully deployed datagrid reading an XML file and showing the data: Private Function MakeDataView() as DataView Dim myDataSet As New DataSet()...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.