Preface
A GridView is a graphical control element that presents a irregular view of data. A normal Grid View similarly supports some or all of the postdating
Clicking a column title to change the kind order of the Grid.
Dragging column heads to change their size and their order.
The GridView regulator displays the valuations of a data origin in a table. Each line represents a clearing, while each row represents a history.
Support to data source controls, similar as SqlDataSource.
Support kind capabilities.
Support update and cancel capabilities.
Support paging capabilities.
Support row selection capabilities.
Law-behind point access to the GridView object model to stoutly set parcels, handle events, and so on.
Numerous crucial fields.
Numerous data fields for the hyperlink columns.
Tailored style layout through themes and styles using CSS and javascript.
The following operations can be performed using GridView control inASP.NET usingC# law before.
Point to Bind data to GridView column
Point to Edit data in GridView
Point to Cancel rows from GridView
Point to Update row from a database
Note
The most important point that we can get by using GridView events are-RowEditing, RowUpdating, RowDeleting, RowCommand, RowDataBound, RowCancelingEdit, and Pagination.
Fields
BoundColumn To control the order and picture of columns.
HyperLinkColumn Presents the bound data in HyperLink controls
ButtonColumn Bubbles a stoner command from within a row to the grid event tutor
TemplateColumn Controls which controllers are gave up in the line
CommandField Displays Edit, Update, and revoke bangers
in take to fluxes in the
.
GridView control's EditItemIndex property.
Details Of Fields
By explicitly creating a BoundColumn in the Grid's Columns collection, the order and picture of each column can be controlled. In the BoundField parcels, when the DataField and the SortExpressions are given, sorting and rendering the data can be fluently done.
A HyperLinkColumn presents the bound data in HyperLink controllers. This is generally used to navigate from an item in the grid to a Details view on another runner by directly assigning the runner URL in NavigationUrl or by rendering it from the database.
With a TemplateColumn, the controllers which are gave up in the file and the data fields bound to the controls can be controlled. By using the TemplateColumn, any type of data control can be fitted.
The EditCommandColumn is a special column type that supports in- place editing of the data in one row in the grid. EditCommandColumn interacts with another plat of the grid EditItemIndex. By dereliction, the value of EditItemIndex is-1, meaning none of the rows ( particulars) in the grid are beingedited.However, an"edit" button is displayed in the EditCommandColumn for each of the rows in the grid, If EditItemIndex is-1.
When the"edit" selector is communed, the grid's EditCommand episode is chucked. It's over to the programmer to handle this event in the law. The typical sense sets EditItemIndex to the named row and also rebinds the data to the grid.
When EditItemIndex is set to a particular row, the EditCommandColumn shows"update"and" cancel" buttons for that row ("edit"is still displayed for the other rows). These buttons beget the UpdateCommand and CancelCommand events to be thrown, independently.
PageIndexChanging event occurs when the property of the grid AllowPaging is set to true, and in the law behind the PageIndexChanging event is fired.
Paging in GridView is allowed by sitting AllowPaging to real.
Paging in The GridView provides the means to display a group of records from the data source (for illustration, the first 20), and also navigates to the" runner" containing the coming 20 records, and so on through the data.
When enabled, the grid will display runner navigation buttons either as" coming/ former" buttons or as numeric buttons. When a runner navigation button is clicked, the PageIndexChanged event is thrown. It's over to the programmer to handle this event in the law.
The GridView fires the RowCommand episode when any push button is pressed. We can give any name as command name, and grounded on that, the check will be done and the circle will be executed.
The GridView fires the RowCreate episode when a substitute row is created.
The GridView fires the RowDeleting event when the command name is given as Cancel.
The GridView fires the RowUpdating episode when the order title is given as Update.
The GridView fires the RowEditing event when the commandment title is given away as Edit.
The GridView fires the RowDatabound episode when a data row is bound to data in a GridView control.
Data in a Grid is generally sorted by clicking the title of the column to sort. Sorting in a DataGrid can be allowed by sitting AllowSorting to real. When enabled, the grid renders LinkButton controls in the title for each column. When the button is clicked, the grid's SortCommand episode is fired. It's over to the programmer to handle this event in the law. Because DataGrid always displays the data in the same order it occurs in the data source, the typical sense sorts the data source and also rebinds the data to the grid.
CRUD operation in Gridview
CRUD is the short form of Produce, Recoup, Update, Cancel operation. Produce, Recoup, Update, Cancel operation used in database data operation. Using these 4 operations we can perform insert, elect, modernize, cancel SQL Garçon Database table data. We'll use these 4 operations in Gridview control inASP.Net.
Gridview
Gridview control helps to show all SQL garçon table data in the list format. After getting the data from the SQL garçon you have to bind that list with Gridview control. For binding data with Gridview use the following 2 lines
Gridview1.DataSource = dt;
Gridview1.DataBind();
Now use the following GridView event parcels to perform events similar as update, cancel, edit cancel and so on. Let us see what the parcels are
DataKeyNames This property I've used to the row indicator of GridView
OnRowEditing This property is used to handle the event when the stoner clicks on the edit button
OnRowCancelingEdit This property is used to handle the event when the stoner clicks on the Cancel button that exists after clicking on the edit button
OnRowDeleting This property is used to handle the event when the stoner clicks on the delete button that deletes the row of the GridView
OnRowUpdating This property is used to handle the event when the stoner clicks on the update button that updates the Grid Record
Websites frequently display thousands of data in a GridView inASP.Net. Generally, the admin can view the registered druggies on the website, but when an admin wants to edit or cancel any fraud or duplicate or damaged data from the table there's a system in GridView to edit, cancel and modernize. See the note section below.
Source Code
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
-
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Untitled Page</title>
- <style type="text/css">
- .Gridview {
- font-family: Verdana;
- font-size: 10pt;
- font-weight: normal;
- color: black;
- }
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" EnableModelValidation="True">
- <Columns>
- <asp:BoundField DataField="id" HeaderText="S.No." />
- <asp:BoundField DataField="name" HeaderText="Name" />
- <asp:BoundField DataField="address" HeaderText="address" />
- <asp:BoundField DataField="country" HeaderText="Country" />
- <asp:CommandField ShowEditButton="true" />
- <asp:CommandField ShowDeleteButton="true" />
- </Columns>
- <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
- <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
- <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
- <RowStyle BackColor="White" ForeColor="#003399" />
- <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
- </asp:GridView>
- </div>
- <div>
- <asp:Label ID="lblresult" runat="server"></asp:Label>
- </div>
- </form>
- </body>
- </html>
-
Design
The design part will look as in the following image:
Code behind
- using System;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Drawing;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- public partial class _Default: System.Web.UI.Page {
- private SqlConnection conn = new SqlConnection("Data Source=NEHASHAMA;Integrated Security=true;Initial Catalog=rp");
- protected void Page_Load(object sender, EventArgs e) {
- if (!IsPostBack) {
- gvbind();
- }
- }
- protected void gvbind() {
- conn.Open();
- SqlCommand cmd = new SqlCommand("Select * from detail", conn);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
- conn.Close();
- if (ds.Tables[0].Rows.Count > 0) {
- GridView1.DataSource = ds;
- GridView1.DataBind();
- } else {
- ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
- GridView1.DataSource = ds;
- GridView1.DataBind();
- int columncount = GridView1.Rows[0].Cells.Count;
- GridView1.Rows[0].Cells.Clear();
- GridView1.Rows[0].Cells.Add(new TableCell());
- GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
- GridView1.Rows[0].Cells[0].Text = "No Records Found";
- }
- }
- protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) {
- GridViewRow row = (GridViewRow) GridView1.Rows[e.RowIndex];
- Label lbldeleteid = (Label) row.FindControl("lblID");
- conn.Open();
- SqlCommand cmd = new SqlCommand("delete FROM detail where id='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", conn);
- cmd.ExecuteNonQuery();
- conn.Close();
- gvbind();
- }
- protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {
- GridView1.EditIndex = e.NewEditIndex;
- gvbind();
- }
- protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {
- int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
- GridViewRow row = (GridViewRow) GridView1.Rows[e.RowIndex];
- Label lblID = (Label) row.FindControl("lblID");
- //TextBox txtname=(TextBox)gr.cell[].control[];
- TextBox textName = (TextBox) row.Cells[0].Controls[0];
- TextBox textadd = (TextBox) row.Cells[1].Controls[0];
- TextBox textc = (TextBox) row.Cells[2].Controls[0];
- //TextBox textadd = (TextBox)row.FindControl("txtadd");
- //TextBox textc = (TextBox)row.FindControl("txtc");
- GridView1.EditIndex = -1;
- conn.Open();
- //SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
- SqlCommand cmd = new SqlCommand("update detail set name='" + textName.Text + "',address='" + textadd.Text + "',country='" + textc.Text + "'where id='" + userid + "'", conn);
- cmd.ExecuteNonQuery();
- conn.Close();
- gvbind();
- //GridView1.DataBind();
- }
- protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
- GridView1.PageIndex = e.NewPageIndex;
- gvbind();
- }
- protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {
- GridView1.EditIndex = -1;
- gvbind();
- }
- }
Save all or press "Ctrl+S" and hit "F5" to run the page, the page will look as in the following image:
Click on "Edit the GridView", it will display Textboxes in each cell as in the following image:
Edit the value(s) here and click on the Update link, it will update all the data, or to remove it click on the "Delete" link above the image shown.
Note
One note will be helpful for you while describing Columns in GridView if you are using a boundfield then create objects of the control using cells[index] in the rowupdateing event of GridView, but if you are using controls itself like Label or textboxes, etc then use Fincontrol("stringid").
SumathiPosted Mar 25, 2024, 3:40 PM
I have added a couple new colums to the Gridview such as: <Columns> <asp:BoundField DataField="regno" HeaderText="RegNo" /> <asp:BoundField DataField="name" HeaderText="Name" /> <asp:BoundField DataField="class" HeaderText="Class" /> <asp:BoundField DataField="mob" HeaderText="Contact" /> <asp:BoundField DataField="dob" HeaderText="DOB" /> <asp:BoundField DataField="gen" HeaderText="Gender" /> <asp:BoundField DataField="address" HeaderText="Address" /> <asp:CommandField ShowEditButton="true" /> <asp:CommandField ShowDeleteButton="true" /> </Columns> Here's the c# code: protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int regno = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()); GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; Label lblREGNO = (Label)row.FindControl("lblREGNO"); //TextBox txtname=(TextBox)gr.cell[].control[]; TextBox textName = (TextBox)row.Cells[0].Controls[0]; TextBox textc = (TextBox)row.Cells[1].Controls[0]; TextBox textmob = (TextBox)row.Cells[2].Controls[0]; TextBox textdob = (TextBox)row.Cells[3].Controls[0]; TextBox textgen = (TextBox)row.Cells[4].Controls[0]; TextBox textadd = (TextBox)row.Cells[5].Controls[0]; //TextBox textadd = (TextBox)row.FindControl("txtadd"); //TextBox textc = (TextBox)row.FindControl("txtc"); GridView1.EditIndex = -1; cn.Open(); //SqlCommand cmd = new SqlCommand("SELECT * FROM student", conn); SqlCommand cmd = new SqlCommand("update student set name='" + textName.Text + "',address='" + textadd.Text + "',class='" + textc.Text + "',gender='" + textgen.Text + "',contact='" + textmob.Text + "',dob='" + textdob.Text + "'where regno='" + regno + "'", cn); cmd.ExecuteNonQuery(); cn.Close(); gvbind(); //GridView1.DataBind(); } However, i'm getting an error that says: A field or property with the name 'mob' was not found on the selected data source. How do i add new columns to the gridview, as i have added 3 more columns but am not getting the output now?
Jerald Jayaraj DPosted Jul 10, 2022, 8:08 AM
Protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { } not hitting/working
Giuuseppe MolinaroPosted Jul 23, 2021, 10:11 AM
I've found an error . this App Run with HiddenField1.Value = "view" but in Stored procedure don't exist @Action ="view" I've found only @Action='Select' .
Akash PrajapatiPosted Apr 16, 2021, 4:29 AM
Please Provide Code For Edit Column I have Not Required So How To Hide all Edit Column and I have required That Time I have Change Only One So One Of function Create For Editable Column
Ashik RahmanPosted Jul 19, 2019, 2:49 AM
Delete function is not working..!! I don't know the reason.. There is no error message or anything but when it is clicked it just remains same..
Sudha ThakurPosted Jul 4, 2019, 10:05 PM
Connection:<connectionStrings> <add name="strcon" connectionString="Server =DESKTOP-SQH5K1D; database=sudha; integrated security =True"/> </connectionStrings>
Sudha ThakurPosted Jul 4, 2019, 10:04 PM
Namespace:using System.Configuration;using System.Data.SqlClient; using System.Data;
Sudha ThakurPosted Jul 4, 2019, 10:04 PM
Class: static string con = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString.ToString(); SqlConnection sqlcon = new SqlConnection(con); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { gridbind(); } } void gridbind() { sqlcon.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = sqlcon; cmd.CommandText = "select * from reg91"; SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); GRID.DataSource = dt; GRID.DataBind(); sqlcon.Close(); } protected void link_Command(object sender, CommandEventArgs e) { int id = Convert.ToInt32(e.CommandArgument); sqlcon.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = sqlcon; cmd.CommandText = "select * from reg91 where id ="+ id+""; SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); if(dt.Rows.Count>0) { hndid.Value = dt.Rows[0]["id"].ToString(); txt1.Text = dt.Rows[0]["name"].ToString(); TextBox2.Text = dt.Rows[0]["emailid"].ToString(); bool active = Convert.ToBoolean(dt.Rows[0]["active"].ToString()); if(active==true) { actv.Checked = true; } else { actv.Checked = false; } btn.Text = "update"; } } protected void Delete_Command(object sender, CommandEventArgs e) { int id = Convert.ToInt32(e.CommandArgument); sqlcon.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = sqlcon; cmd.CommandText = "delete from reg91 where id =" + id + ""; cmd.ExecuteNonQuery(); sqlcon.Close(); gridbind(); } protected void btn_Click(object sender, EventArgs e) { sqlcon.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = sqlcon; int a = 0; if(actv.Checked==true) { a = 1; } else { a = 0; } string gender1 = ""; if(RadioButton1.Checked==true) { gender1 = "f"; } else if (RadioButton2.Checked == true) { gender1 = "m"; } if (btn.Text == "update") { cmd.CommandText = "update reg91 set name='" + txt1.Text + "',emailid='" + TextBox2.Text + "',country='" + TextBox3.Text + "',city='" + txtbox4.Text + "',gender='" + gender1 + "',active=" + a + " where id =" + Convert.ToInt32(hndid.Value) + ""; } else { cmd.CommandText = "insert into reg91(name,emailid,country,city,gender,active) values('" + txt1.Text + "','" + TextBox2.Text + "','" + TextBox3.SelectedItem.Text + "','" + txtbox4.SelectedItem.Text + "','" + gender1 + "'," + a + ")"; } cmd.ExecuteNonQuery(); sqlcon.Close(); btn.Text = "submit"; hndid.Value = null; txt1.Text = null; TextBox2.Text = null; TextBox3.Text = null; txtbox4.Text = null; actv.Text = null; actv.Checked = false; gridbind(); }
Sudha ThakurPosted Jul 4, 2019, 10:02 PM
Webformdesign:<div> <table> <tr> <td>name</td> <td> <asp:TextBox ID="txt1" runat="server"></asp:TextBox> </td> </tr> <tr> <td>emailid</td> <td> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td> </tr> <tr> <td>country</td> <td> <asp:DropDownList ID="TextBox3" runat="server"> <asp:ListItem Value="2">us</asp:ListItem> <asp:ListItem Value="3">uk</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td>city</td> <td> <asp:DropDownList ID="txtbox4" runat="server"> <asp:ListItem Value="1">kol</asp:ListItem> <asp:ListItem Value="2">del</asp:ListItem> <asp:ListItem Value="3">patna</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td>gender</td> <td> <asp:RadioButton ID="RadioButton1" runat="server" Text="f" GroupName="1"></asp:RadioButton > <asp:RadioButton ID="RadioButton2" runat="server" Text="m" GroupName="1"></asp:RadioButton > </td> </tr> <tr> <td>active</td> <td> <asp:CheckBox ID="actv" runat="server" ></asp:CheckBox> </td> </tr> <tr> <td> <asp:Button ID="btn" runat="server" Text="submit" OnClick="btn_Click"></asp:Button> </td> </tr> </table> <table> <tr> <td> <asp:GridView ID="GRID" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField HeaderText="Edit"> <ItemTemplate> <asp:linkbutton ID="link" runat="server" Text="update" OnCommand="link_Command" CommandArgument='<%#Eval("id") %>'></asp:linkbutton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Delete"> <ItemTemplate> <asp:linkbutton ID="linkdelete" runat="server" Text="Delete" OnCommand="Delete_Command" CommandArgument='<%#Eval("id") %>'></asp:linkbutton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="name"> <ItemTemplate> <asp:Label ID ="lblguserid" runat ="server" Text ='<%#Eval("name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="emailid"> <ItemTemplate> <asp:Label ID ="lblguserid1" runat ="server" Text ='<%#Eval("emailid") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="country"> <ItemTemplate> <asp:Label ID ="lblguserid2" runat ="server" Text ='<%#Eval("country") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="city"> <ItemTemplate> <asp:Label ID ="lblguserid3" runat ="server" Text ='<%#Eval("city") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="active"> <ItemTemplate> <asp:Label ID ="lblguserid4" runat ="server" Text ='<%#Eval("active") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="gender"> <ItemTemplate> <asp:Label ID ="lblguserid5" runat ="server" Text ='<%#Eval("gender") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </td> </tr> </table> </div> <asp:HiddenField ID="hndid" runat="server" />
Sudha ThakurPosted Jul 4, 2019, 4:40 AM
I am getting error in webform
Vishal MishraPosted May 16, 2019, 9:33 AM
Label lbldeleteid = (Label)row.FindControl("lblID"); what is 'lbldeleteid' and 'lblID' con.Open(); I WANT TO DELETE RECORED FROM GRID VIEW USING OR AGAINST MY CLIENTNAME - HOW TO DO IT? SqlCommand cmd = new SqlCommand("DELETE FROM tbl_ClientDetails WHERE sClientNAme='" + Convert.ToInt32(grdView.DataKeys[e.RowIndex].ToString()) + "'", con);
Prashant SrivastavaPosted May 14, 2019, 1:13 PM
Time waste
Karthi SPosted Aug 31, 2018, 6:16 PM
Public partial class billnew : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { displayrecord(); } } protected void Button1_Click(object sender, EventArgs e) { Connect c = new Connect(); c.getcon(); SqlCommand cmd = new SqlCommand("newbill1", c.con); cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text; cmd.CommandType = CommandType.StoredProcedure; cmd.ExecuteNonQuery(); c.con.Close(); } protected DataTable displayrecord() //display { Connect c = new Connect(); c.getcon(); SqlDataAdapter adp = new SqlDataAdapter("select * from billnew", c.con); DataTable dt = new DataTable(); adp.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); return (dt); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; displayrecord(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; displayrecord(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { Connect c = new Connect(); c.getcon(); TextBox t1 = new TextBox(); t1 = (TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]; //TextBox t2 = new TextBox(); t2 = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]; string s = GridView1.DataKeys[e.RowIndex].Value.ToString();//to get id of row SqlCommand cmd = new SqlCommand("update billnew set name='" + t1.Text + "' ,age="+t2.Text+ " where id=" + s + "", c.con); cmd.ExecuteNonQuery(); c.con.Close(); GridView1.EditIndex = -1; displayrecord(); Response.Write("updated successfully"); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { Connect c = new Connect(); c.getcon(); string s = GridView1.DataKeys[e.RowIndex].Value.ToString();//to get id of row SqlCommand cmd = new SqlCommand("delete from billnew where id=" + s + "", c.con); cmd.ExecuteNonQuery(); c.con.Close(); GridView1.EditIndex = -1; displayrecord(); Response.Write("updated successfully"); }
LENIN ROBERTPosted Jun 15, 2018, 7:32 AM
Update function is not working
Bhavesh JadavPosted Mar 28, 2018, 11:31 PM
Good explanation with examples, thanks to share
Dhimesh ParmarPosted Feb 22, 2018, 6:50 AM
How To Use With Radiobutton
Rashmi PawarPosted Nov 27, 2017, 5:24 AM
I need a gridview in which only the id's will be present and by clicking on an id we should get the details in the same gridview. Can anyone please help me with the code
Inżynier UmairPosted Oct 22, 2017, 8:29 AM
What about if u provide another coloumn which contains images of that user and the number of images are may be more than one or two or three... So My question is In that case how u bind those multiple images with that particular user in gridview or any other div tag .
Shubham AgrawalPosted Sep 15, 2017, 6:01 AM
TextBox textName = (TextBox)row.Cells[0].Controls[0];Additional information: Unable to cast object of type 'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'.
Sachin Singh RajputPosted Aug 23, 2017, 2:25 PM
Neha this article is very very useful. Please u have any article for learning MVC then please paste URL/link in comment or mail me on [email protected] plz
Inshu MalhotraPosted Jun 24, 2017, 4:09 AM
Int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()); I am facing this error "Index out of bound exception"
morteza talebpourPosted Jun 19, 2017, 11:01 AM
Your code with Errors...
Sarath ChandraPosted Jun 13, 2017, 7:20 AM
Thank u very much! Glad to have your code and make our way for updating and deleting contents in data table EASY and FLEXIBLE
samudraiah 19231Posted May 25, 2017, 10:02 AM
Please upload if u click save then registration from data go to gridview and and then edit the gridview The selected row go to text box upload this type exaple
Nasir AliPosted May 18, 2017, 9:09 AM
I am thankful to you for sharing knowledge.
Adesh MishraPosted May 11, 2017, 7:18 AM
I am not able to update, due to control getting old value onRowUpdating Event.
Manav PandyaPosted Mar 14, 2017, 12:10 AM
Thanks for sharing ma'm....
Govinda MundadaPosted Mar 13, 2017, 2:11 PM
Hi Neha, Would you please let us know the schema of detail table...?
Mani RanaPosted Mar 7, 2017, 6:56 AM
Easy to learn and clear explained
Manav PandyaPosted Sep 17, 2016, 10:00 AM
Clear and well explained mam ...
Ramesh PalaniappanPosted Aug 19, 2016, 3:40 AM
Nice
Ghanshyam KumarPosted Jul 12, 2016, 10:21 AM
Can i download your source code ? pls provide a download link
Ghanshyam KumarPosted Jul 12, 2016, 10:20 AM
This is nice article ,
kalu singh raoPosted Jul 7, 2016, 1:40 AM
Nice...
Thiruppathi RPosted Jun 3, 2016, 2:43 AM
Nice sharing..
Ashish ShettyPosted Apr 25, 2016, 1:13 AM
Hi Neha...How you getting the FindControl("lblID"). when its a templatecolumn
Asfend YarPosted Feb 29, 2016, 10:49 AM
nice
Rupali ShindePosted Oct 16, 2015, 5:48 AM
nice
Pankaj Kumar ChoudharyPosted Sep 17, 2015, 1:14 PM
Nice Explain.......
Karthikeyan KPosted Aug 25, 2015, 6:19 AM
Good one
Upendra Pratap ShahiPosted Aug 6, 2015, 6:56 AM
nice..
Atul RawatPosted Jul 29, 2015, 3:03 AM
nice article
sandeep palPosted Mar 7, 2015, 11:20 AM
please
sandeep palPosted Mar 7, 2015, 11:20 AM
.I am having trouble in updating the records. When I click the update button nothing happens. I am pretty sure I have used the code correctly. Can u help me?
Akash TiwariPosted Dec 9, 2014, 6:45 AM
Thanks it's very helpfull
sagar agarwalPosted Oct 24, 2014, 7:28 AM
hi neha....I am having trouble in updating the records. When I click the update button nothing happens. I am pretty sure I have used the code correctly. Can u help me?
mahdi mahdiPosted Sep 3, 2014, 8:02 AM
thanks.............from iran
Ranjeet PatilPosted Aug 24, 2014, 6:20 AM
GridView Edit Update Using BoundField http://patilranjeet.blogspot.in/p/aspnet-gridview-edit.html
Dheeraj PandeyPosted Aug 12, 2014, 5:44 AM
very helpful article for me....help of this article I have done Edit and Delete, Thank u mam
pervejkumar munjalPosted Jun 21, 2014, 11:19 AM
http://www.getproductprice.com
pervejkumar munjalPosted Jun 21, 2014, 11:18 AM
http://www.interviewqsn.com
pervejkumar munjalPosted Jun 21, 2014, 11:18 AM
Hi below are some good sites too
Jennifer CrisPosted Apr 16, 2014, 3:54 AM
hi i have converted this code to vb. im having errors could you help
Nitin SharmaPosted Mar 27, 2014, 6:27 AM
is anybody help me for this how we can make this coding workable SQL Query create database test use test create table gridview(id int identity(1,1),ename varchar(50),city varchar(50)) insert into gridview values('nitin','hisar') insert into gridview values('deepak','gwalior') insert into gridview values('raman','delhi') select*from gridview update gridview set ename='nitin sharma', city='haryana' where id=1 default.aspx <asp:GridView ID="GridView1" runat="server" Height="189px" Width="332px" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="id" onrowcancelingedit="GridView1_RowCancelingEdit" onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"> </asp:GridView> default.aspx.cs protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value); string name=GridView1.Rows[e.RowIndex].Cells[0].Text; string city=GridView1.Rows[e.RowIndex].Cells[1].Text; if (con.State == ConnectionState.Closed) { con.Open(); } SqlCommand cmd = new SqlCommand("update gridview set ename='" name "', city='" city "' where id='" id "'", con); cmd.ExecuteNonQuery(); con.Close(); GridView1.DataBind(); display(); } can you please check my code and tell me why this code not work correctly
Mukesh Kumar TiwariPosted Mar 27, 2014, 6:11 AM
Very simple and useful article........nice.....
aparna shelkePosted Mar 22, 2014, 1:23 AM
i got erroe"DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'id'." what sould i do??
Sapna KansayPosted Mar 19, 2014, 4:21 PM
thank you...
piya pPosted Mar 18, 2014, 3:27 AM
Hi,Thanx it is very good post.I have one query How can we update image instead of text using above code?
Alisha MPosted Aug 17, 2013, 1:47 AM
Wen i try to edit the grid row i get error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
Sandeep ChaurasiaPosted Jul 26, 2013, 11:38 AM
Very nice article its good work
Neha SharmaPosted Jun 6, 2013, 9:14 AM
ok ,try these links : http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html , http://www.codeproject.com/Articles/467788/Dynamically-adding-and-deleting-rows-from-ASP-NET ,
Bhavneet LakhoriaPosted Jun 6, 2013, 7:48 AM
Neha I ahve a GridView with tha Fields Name Question ,POstBy,PostOn,Description ANd a Reply Link Lebal..When i click on Reply Button Then A New Row of Reply would b Created Which Has Reply,Reply By,Reply On
Bhavneet LakhoriaPosted Jun 6, 2013, 6:47 AM
Neha Wt About mine Problem ? How to Create a GridView Like On Forum Pages.
Shaily kumarPosted Jun 6, 2013, 6:06 AM
Hii Neha...Thanks once again....i just want to ask that how can we add a dialogue box before deleting the record in this code???
Bhavneet LakhoriaPosted Jun 4, 2013, 12:15 AM
Bt wt about mine Problem Mis. Neha ?
Shaily kumarPosted Jun 3, 2013, 7:58 AM
Yes...My problem is solved completely..Thanku soo much for your help & support....Your Code Rocks!!!!!
Neha SharmaPosted Jun 3, 2013, 7:02 AM
hi Shaily,let me know about your problem is that solved or not?
Shaily kumarPosted Jun 3, 2013, 2:08 AM
Hey neha check this link: http://www.c-sharpcorner.com/Blogs/11842/how-to-edit-delete-update-or-cancel-record-in-gridview-usi.aspx
Neha SharmaPosted Jun 1, 2013, 7:06 AM
As discussed send me the code aspx as well update
Shaily kumarPosted Jun 1, 2013, 6:27 AM
Hii Neha , the code is really helpful but it is showing error:Unable to cast object of type 'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'. in update function. Please help me.
RAJNISH BAKSHIeditedPosted May 10, 2013, 12:37 AMEdited May 10, 2013, 12:38 AM
Thanks Neha, it works for me, can u describe that how to set the width of the TextBox in editable mode.. thanks in advance
Neha SharmaPosted May 8, 2013, 1:33 AM
Sure Abhimanyu..take your time.
Abhimanyu SinghPosted May 8, 2013, 12:06 AM
hii Neha thax for this let me try it if i wil have any problebm i will ask you
Neha SharmaPosted May 7, 2013, 1:46 AM
Hi Abhimanyu,You can do this by export Gridview data in to pdf format and then can take printout as well can save each files.If you want to go through with this solution go here : http://www.c-sharpcorner.com/UploadFile/9f0ae2/export-gridview-into-pdf-in-Asp-Net/
Abhimanyu SinghPosted May 6, 2013, 1:54 AM
Hii Neha, hope you are doing gr8 i need help once again form your side as you did earlier actually, I want the bulk data print(one by one all data from DB on dropdownlist Search/selection)from DataBase on dropdown select event....without gridview display only want print out in fixed format..
Neha SharmaPosted Apr 30, 2013, 1:47 AM
Hi Bhavneet,Please let me know "PROBLEM" or try to add brekapoint and filter your problem yourself.
Neha SharmaPosted Apr 30, 2013, 1:46 AM
Hi srikant,learn the validations in gridview http://forums.asp.net/t/1376279.aspx/1
Neha SharmaPosted Apr 30, 2013, 1:44 AM
Hi Neeraj,Youc an fetch values from gridviews based on datakey and when you have values you can show anywhere.
Bhavneet LakhoriaPosted Apr 29, 2013, 1:07 PM
I Have a Problem while updating my gridview.
srikanth keditedPosted Apr 27, 2013, 3:46 AMEdited Apr 27, 2013, 3:48 AM
VERY GOOD ARTICLE:)now i need to give validation while editing in the text box.it should allow only char,int plz help on this
Niraj MistryPosted Apr 23, 2013, 6:00 AM
hi..., I have add crystal report..Work to use ok but how can I Value pass thru Parameter and SelectionFormulas in Crystal 9. so if u some little more help me..?
Neha SharmaPosted Mar 2, 2013, 6:09 AM
if you tried that let me know what kind of problem you are facing with that?
Abhimanyu SinghPosted Mar 2, 2013, 6:07 AM
yeah exactally like that i tried also but did not get it now can you help me Plz thx
Neha SharmaPosted Mar 1, 2013, 3:22 AM
Abhimanyu i think now you want to show image pop-up to show large size of images?? if yes visit my blog recently i psted how to do that.thx
Neha SharmaPosted Mar 1, 2013, 3:21 AM
thx sreejesh for you kind words!!
Abhimanyu SingheditedPosted Feb 27, 2013, 4:19 AMEdited Feb 27, 2013, 4:21 AM
Hii neha, I displayed images inside grid row but i reduced its size as thumbnails because there alignment is going too large.So is it possible to show actual size on clicking on their thumbnails. Sorry for my English i think you got it...
Sreejesh SPPosted Feb 27, 2013, 2:05 AM
Dear Neha, Your code is correct,but this is not use in applcation level,this is used only Accademicsection Thanks Sreejsh
Neha SharmaPosted Feb 26, 2013, 6:26 AM
Sure
Abhimanyu SinghPosted Feb 26, 2013, 6:21 AM
Thanks for gr8 Suggestion I am trying now after that i will ask you please.
Neha SharmaPosted Feb 26, 2013, 2:51 AM
Hi abhimanyu,while binding a gridview you can take a image control as the fields we have taken in asp:boundfields,and mention the filed name in datafield propery.hope you got it now.anywyas i would also suggest you to post your query in forum section,so that everyone can help you,else youc an ask it here also ,i would love to help if i can.
Abhimanyu SingheditedPosted Feb 26, 2013, 2:35 AMEdited Feb 26, 2013, 2:38 AM
Hii, thax Neha i got it now Could you help me to display the image in grid row according to their id from DB Actually i am new in this field.So i dont have theoretical concept.
Neha SharmaPosted Feb 26, 2013, 1:14 AM
Hi Kedar,it's nice to hear that you like this article,but for your question truly speaking i read your query many time but i am unable to understand what do you exactly want to do?I request you to please elaborate it clearly and then post it in forum section,i ll try to definitely help you.Thnaks
kedar pawgieditedPosted Feb 25, 2013, 9:49 AMEdited Feb 25, 2013, 9:50 AM
Hi I read your article its really nice one. I have a question. If you go thru the article of the following link and in the end the data is added using the textboxes or edited using them. -- http://www.c-sharpcorner.com/UploadFile/satyapriyanayak/update-records-of-a-table-through-differente-page-in-Asp-Net/ -- Can u help me in designing a form where i have to make a sales invoice where i have to add multiple items, but i have to select proper packaging of the item. It will be like this...Select :-- Item Name, then add all companies which provide this item in select company combo box and in combobox leave event popup a window with a query like item + company (the result will give the details about item with different packaging like 1 lit,2 lit or 5 lit bottles. and select one from it and update into datagrid. Just need about how i should arrange these things using textbox grids (for showing and updating)in desktop application in c#
Neha SharmaPosted Feb 25, 2013, 12:35 AM
Hi abhimanyu,it's nice to hear that it works for you,i used it to work on the basis of seelcted id ,i fetch selected value in the variable userid.
Abhimanyu SinghPosted Feb 25, 2013, 12:02 AM
Thax a lot for this Article it is really helpful for me.Only i didnot get about lable because you have taken it single in designing page with different ID and coded with different id in coding area any way i managed it and running well.if you dont mind can you explain the why u taken "'where id='"+userid+"'"
Neha SharmaeditedPosted Feb 21, 2013, 12:37 AMEdited Feb 21, 2013, 12:38 AM
thx Vithal and Ancel cool,where r u getting this error,in delete section or in update section,did you modified the code for some data according to you to your requirement such as table name fields and so on??
ancel coolPosted Feb 20, 2013, 3:03 AM
I get error T_T this sintak cant read, but nothing error msg in VS.. [quote] da.Fill(ds); [/quote] msg.. Incorrect syntax near 'tabelform'. why ? :(