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

Delete Row in GridView

40
@Frinavale
ok,i managed to do this.now im having a problem deleting a record.can you tel me what to do.here's my code
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="GridView1" runat="server"  onrowdeleting="GridView1_RowDeleting" >
  2.   <Columns>
  3.      <asp:CommandField ShowDeleteButton="True" HeaderText="Delete">
  4.       <HeaderStyle HorizontalAlign="Left" />
  5.       </asp:CommandField>
  6.   </Columns>                            
  7. </asp:GridView>

Expand|Select|Wrap|Line Numbers
  1. protected void btnSaveRef_Click(object sender, EventArgs e)
  2.     {
  3.         //if (txtUserName.Text.Trim() == "")
  4.         //{
  5.         //    this.lblTips.Text = "You must fill a username.";
  6.         //    return;
  7.         //}
  8.         //else
  9.         {
  10.             AddDataToTable(this.txtRefFName.Text.Trim() + " " + this.txtRefMName.Text.Trim() + " " + this.txtRefLName.Text.Trim(), this.txtRefAddress.Text.Trim(), (DataTable)Session["myDatatable"]);
  11.  
  12.             this.GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
  13.             this.GridView1.DataBind();
  14.  
  15.             this.txtRefFName.Text = "";
  16.             this.txtRefAddress.Text = "";
  17.             this.txtRefMName.Text = "";
  18.             this.txtRefLName.Text = "";
  19.             //this.txtUserName.Text = "";
  20.             //this.lblTips.Text = "";
  21.         }
  22.     }
  23.  
  24.     private DataTable CreateDataTable()
  25.     {
  26.         DataTable myDataTable = new DataTable();
  27.  
  28.         DataColumn myDataColumn;
  29.  
  30.         //myDataColumn = new DataColumn();
  31.         //myDataColumn.DataType = Type.GetType("System.String");
  32.         //myDataColumn.ColumnName = "id";
  33.         //myDataTable.Columns.Add(myDataColumn);
  34.  
  35.         myDataColumn = new DataColumn();
  36.         myDataColumn.DataType = Type.GetType("System.String");
  37.         myDataColumn.ColumnName = "name";
  38.         myDataTable.Columns.Add(myDataColumn);
  39.  
  40.         myDataColumn = new DataColumn();
  41.         myDataColumn.DataType = Type.GetType("System.String");
  42.         myDataColumn.ColumnName = "address";
  43.         myDataTable.Columns.Add(myDataColumn);
  44.  
  45.  
  46.  
  47.         return myDataTable;
  48.     }
  49.  
  50.     private void AddDataToTable(string name, string address, DataTable myTable)
  51.     {
  52.         DataRow row;
  53.  
  54.         row = myTable.NewRow();
  55.  
  56.         //row["id"] = Guid.NewGuid().ToString();
  57.         row["name"] = name;
  58.         row["address"] = address;
  59.  
  60.  
  61.         myTable.Rows.Add(row);
  62.     }
  63.  if (!Page.IsPostBack)
  64.         {
  65.   DataTable myDt = new DataTable();
  66.             myDt = CreateDataTable();
  67.             Session["myDatatable"] = myDt;
  68.  
  69.             this.GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
  70.             this.GridView1.DataBind();
  71.  
  72.  
  73.         }
  74.  
Jul 1 '10 #1
3 2085
Frinavale
9,735 Expert Mod 8TB
I don't see any C# code that handles the delete?
You posted code for handling the "btnSaveRef", and "CreateDataTable", and "AddDataToTable"...

-Frinny

Please try to post 1 question per thread :)
New questions should go in their own thread.
Jul 1 '10 #2
lisles
40
@lisles
i'l keep that in mind...
i dont know how to do the delete.can you suggest some way?
Jul 2 '10 #3
Frinavale
9,735 Expert Mod 8TB
Well the first step would be to implement a method that handles the GridView.RowDeleting event. In that method you need to connect to the database and delete the record that matches the row that is being deleted in the GridView. After that you need to refresh the datasource for the GridView to show the most current data (the row deleted).

-Frinny
Jul 6 '10 #4

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

Similar topics

3
by: | last post by:
Hello, I have created an ASP.NET 2.0 application that utilized a Gridview Control to display and update/delete data. The problem I am having is that the gridview control is displaying the data...
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....
1
by: Mike P | last post by:
When you use a SqlDataSource to hook up data to a GridView it seems to be to be very inflexible. For example, I want to create my own Delete button with my own code and I also want to create a...
0
by: Steve | last post by:
I have a gridview which uses an objectdatasource for its select and delete. The delete command uses the function below. The delete itself works but the extra logic which requires parameters...
2
by: needin4mation | last post by:
In a GridView, take for example a delete button. When I click the delete button in my GridView the GridView row is deleted and the GridView rebinds and refreshes the records to show accurately. ...
4
by: Wannabe | last post by:
I am using ASP.Net 2.0 and have a gridview on my page. I have everything working except the delete command. The page reloads except the row I am trying to delete is still there. I believe it is...
11
by: Ed Dror | last post by:
Hi there, I'm using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro. I have a Price page (my website require login) with GridView with the following columns PriceID, Amount, Approved,...
2
by: Michael | last post by:
It seems that a gridview allows us to delete only a single row at a time. How to extend this functionality to select multiple rows and delete all of the selected rows in a single stroke? just like...
4
by: Jonathan Wood | last post by:
Greetings, I have a GridView control. For a couple of reasons, I ended up with a TemplateField to contain my Edit and Delete links, something like this: <asp:TemplateField ShowHeader="False">...
25
by: premprakashbhati | last post by:
i want to have checkboxes in my gridview ...by checking it ,i can delete the row/rows.... so i have written code and got 2 Errors ...can anyone solve it... Error:1...'gridview.RetrieveItems()': not...
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: 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,...

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.