473,608 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding rows in gridview

81 New Member
Hi...

Iam using GridView in asp.net(2.0) .My requirement is user can add,update,dele te in gridview I written the code for add & update but can u help adding rows in grid view

My code:


<asp:GridView ID="GridView1" runat="server" AutoGenerateCol umns="False" CellPadding="3" Height="237px" Style="z-index: 100; left: 155px;
position: absolute; top: 132px" Width="688px" BackColor="Whit e" BorderColor="#C CCCCC" BorderStyle="No ne" BorderWidth="1p x" OnRowCancelingE dit="GridView1_ RowCancelingEdi t" OnRowDeleting=" GridView1_RowDe leting" OnRowEditing="G ridView1_RowEdi ting" OnRowUpdating=" GridView1_RowUp dating">
<FooterStyle BackColor="Whit e" ForeColor="#000 066" />
<RowStyle ForeColor="#000 066" />
<SelectedRowSty le BackColor="#669 999" Font-Bold="True" ForeColor="Whit e" />
<PagerStyle BackColor="Whit e" ForeColor="#000 066" HorizontalAlign ="Left" />
<HeaderStyle BackColor="#006 699" Font-Bold="True" ForeColor="Whit e" />
<Columns>
<asp:TemplateFi eld>
<HeaderTemplate >
<asp:CheckBox ID="chk" runat="server" />
</HeaderTemplate>
<ItemTemplate >
<asp:CheckBox ID="chk1" runat="server" />
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Emp ID">
<ItemTemplate >
<asp:Label ID="lbl1" runat="server" Text='<%#Eval(" EID") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Emp Name">
<ItemTemplate >
<asp:Label ID="lbl2" runat="server" Text='<%#Eval(" EName") %>' ></asp:Label>
</ItemTemplate>
<EditItemTempla te>
<asp:TextBox ID="tb1" Text='<%#Eval(" EName") %>' runat="server"> </asp:TextBox>
</EditItemTemplat e>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Emp Dept">
<ItemTemplate >
<asp:Label ID="lbl3" runat="server" Text='<%#Eval(" EDept") %>' ></asp:Label>
</ItemTemplate>
<EditItemTempla te>
<asp:TextBox ID="tb2" Text='<%#Eval(" EDept") %>' runat="server"> </asp:TextBox>
</EditItemTemplat e>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Cit y">
<ItemTemplate >
<asp:Label ID="lbl4" runat="server" Text='<%#Eval(" City") %>' ></asp:Label>
</ItemTemplate>
<EditItemTempla te>
<asp:TextBox ID="tb3" Text='<%#Eval(" City") %>' runat="server"> </asp:TextBox>
</EditItemTemplat e>
</asp:TemplateFie ld>
<asp:CommandFie ld ShowEditButton= True ShowDeleteButto n=True CancelText="Qui t" />
</Columns>
</asp:GridView>


CodeWindow:

using System;
using System.Data;
using System.Configur ation;
using System.Collecti ons;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
using System.Data.Sql Client;

public partial class grid : System.Web.UI.P age
{
SqlConnection cn;
DataSet ds;
SqlDataAdapter da;
SqlCommandBuild er bldr;

protected void Page_Load(objec t sender, EventArgs e)
{

}
protected void LinkButton1_Cli ck(object sender, EventArgs e)
{
getdata();
}
public void getdata()
{
cn = new SqlConnection(" user id=sa;password= connex;database =santhosh;data source=BLRWS06C ONNEXIN\\SQLEXP RESS");
cn.Open();
ds = new DataSet();
da = new SqlDataAdapter( "select * from empdetails", cn);
da.Fill(ds, "details");
GridView1.DataS ource = ds.Tables[0];
GridView1.DataB ind();
}
protected void GridView1_RowEd iting(object sender, GridViewEditEve ntArgs e)
{
GridView1.EditI ndex = e.NewEditIndex;
getdata();

}
protected void GridView1_RowCa ncelingEdit(obj ect sender, GridViewCancelE ditEventArgs e)
{
GridView1.EditI ndex = -1;
getdata();
}
protected void GridView1_RowUp dating(object sender, GridViewUpdateE ventArgs e)
{
Label l1 = (Label)GridView 1.Rows[e.RowIndex].FindControl("l bl1");
TextBox t1 = (TextBox)GridVi ew1.Rows[e.RowIndex].FindControl("t b1");
TextBox t2 = (TextBox)GridVi ew1.Rows[e.RowIndex].FindControl("t b2");
TextBox t3 = (TextBox)GridVi ew1.Rows[e.RowIndex].FindControl("t b3");


string str = "update empdetails set EName='" + t1.Text + "',EDept='" + t2.Text + "',City='" + t3.Text + "' where EID=" + l1.Text;

cn = new SqlConnection(" user id=sa;password= connex;database =santhosh;data source=BLRWS06C ONNEXIN\\SQLEXP RESS");
cn.Open();
SqlCommand cmd = new SqlCommand(str, cn);
cmd.ExecuteNonQ uery();
GridView1.EditI ndex = -1;
getdata();
}
protected void GridView1_RowDe leting(object sender, GridViewDeleteE ventArgs e)
{
Label l1 = (Label)GridView 1.Rows[e.RowIndex].FindControl("l bl1");
TextBox t1 = (TextBox)GridVi ew1.Rows[e.RowIndex].FindControl("t b1");
TextBox t2 = (TextBox)GridVi ew1.Rows[e.RowIndex].FindControl("t b2");
TextBox t3 = (TextBox)GridVi ew1.Rows[e.RowIndex].FindControl("t b3");


string str = "delete from empdetails where EID=" + l1.Text;

cn = new SqlConnection(" user id=sa;password= connex;database =santhosh;data source=BLRWS06C ONNEXIN\\SQLEXP RESS");
cn.Open();
SqlCommand cmd = new SqlCommand(str, cn);
cmd.ExecuteNonQ uery();
GridView1.EditI ndex = -1;
getdata();
}
}
Sep 28 '07 #1
0 1959

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

Similar topics

4
8743
by: VMI | last post by:
I'm working on a questionaire, and I'd like to add the questions to a gridview, so that each row (of two "columns" each) has the question in one field, and the editable section (where the user will enter the answer) in the next field. The questions are not stored in a table, and I'm thinking of saving the answers manually (i.e. no datasource). Is this possible? This is a web app using asp.net 2.0. THanks.
1
1246
by: Loading name... | last post by:
hey asp.net 2.0 Is it possible to add records to a database via a GridView, I mean in the GridView the user add new rows which are saved back to the database? I know I can edit rows, but I'm not sure it's possible to add NEW rows - I'm a newbie
1
4354
by: JB | last post by:
I'm dynamically creating a Gridview object and filling it with contents from an ArrayList as follows: GridView2.AutoGenerateColumns = true; GridView2.DataSource = ArrayListObject; GridView2.DataBind(); This works nicely, but i need to add a button field row onto it, as well as possibly a textbox and image field later.
2
2108
by: Blasting Cap | last post by:
I've got a gridview (that I converted over from a datagrid, which had been working properly), that is doubling up the number of rows returned. When it was running as a datagrid, the same code sent back the proper number of rows. The only thing different I am doing is to display the number of rows returned in the footer of the gridview. The Bindgrid is as follows: Sub BindGrid() Session("reportlevel") = Session("availabilityrptlevel")
1
3029
by: bJames | last post by:
Hi I've been all around looking for an answer to this question, but can't find it. I've got a gridview with a single unbound column. It's got a textbox and button in the footer row. I"m trying to get the value from the textbox to add to the gridview on the button click. After trying to integrate other solutions into my code I've come up with this garbled mess in the button click event: DataTable paramValues = new DataTable(); ...
2
5329
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 what hotmail web UI is doing now (having the option of selecting multiple rows (using the checkbox provided) and perform a set of operations on them)
1
1841
by: COHENMARVIN | last post by:
I have a gridview and I need to know the number of rows in it. I find the Page_load and the Gridview_Load events are too early to find out the number of rows. I can use the Gridview_databound event to check for the number of rows. But the problem is that the user can update a field on my form, which reduces the number of rows in the gridview, because the gridview SELECT statement no longer finds the records when their flag has been set...
2
1560
by: shrikanthn | last post by:
Hi All, i am adding some data into GridView (adding 4 to 5 rows) at runtime in asp.net page using c#. but getting outofbond error.
7
6189
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
I'm adding subheadings to a gridview. Each sub head has a few link buttons. I'm adding the controls in the rowdatabound event code follows: sorry about the length here. I have to be missing something. The buttons show up and post back, but the events do not fire. any help would be appreciated!!! Thank you. protected void gvEntitiesRowDataBound(object sender, GridViewRowEventArgs e) {
0
8002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8496
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8475
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8148
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8338
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4024
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1329
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.