dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersMani
Jivani
Tanya
Vinoth Swaminathan
Sriram
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » DataGridView

Displaying,Adding,Updating,Editing,Deleting data of a Grid


Posted Date:     Category: DataGridView    
Author: Member Level: Gold    Points: 10



 


Html Code:

Place the below tag inside the <form> tag

<table width=100% cellpadding=0 cellspacing=0>
<tr><td colspan=2></td></tr>
<tr><td width=50%><asp:datagrid id="DataGrid1" runat="server"
Width="328px" Height="304px" AutoGenerateColumns="False" BorderColor="#E7E7FF" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="3" GridLines="Horizontal" tabIndex="10" onselectedindexchanged="DataGrid1_SelectedIndexChanged">
<SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
<ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="eno" HeaderText="Emp no"></asp:BoundColumn>
<asp:BoundColumn DataField="ename" HeaderText="Ename"></asp:BoundColumn>
<asp:BoundColumn DataField="dept" HeaderText="Dept"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id="lnkbtnEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id="lnkbtnDelete" runat="server" CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
</asp:datagrid></td>
<td>



<table width=100%>
<tr><td>Eno</td><td><asp:textbox id="TextBox1" runat="server" tabIndex="2" ontextchanged="TextBox1_TextChanged"></asp:textbox></td></tr>
<tr><td>Name</td><td><asp:textbox id="TextBox2" runat="server" tabIndex="1" ontextchanged="TextBox2_TextChanged"></asp:textbox></td></tr>
<tr><td>Dept</td><td><asp:textbox id="TextBox3" runat="server" MaxLength="4" ontextchanged="TextBox3_TextChanged"></asp:textbox></td></tr>
<tr><td> </td></tr>
<tr><td><asp:button id="Button3" runat="server" Text="UPDATE" tabIndex="5" onclick="Button3_Click"></asp:button>
<asp:button id="Button4" runat="server" Text="CANCEL" tabIndex="6" onclick="Button4_Click"></asp:button></td><td><asp:label id="Label5" runat="server">Label</asp:label></td></tr>
</table>
</td>
</tr>
</table>



for .CS file:

private static SqlConnection con;
private static SqlCommand sqlcom;

protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
FillGrid(con);
}
Button3.Visible=false;

}

private void setConnection()
{
con = new SqlConnection();
if(con.State == ConnectionState.Closed)
{
con =new SqlConnection(ConfigurationManager.AppSettings["connection string"].ToString());
con.Open();
}
}


private void FillGrid(SqlConnection sqlcon)
{

setConnection();
DataSet ds = new DataSet();
SqlDataAdapter da=new SqlDataAdapter("select * from emp",sqlcon);
da.Fill(ds,"DATA_TABLE");
DataGrid1.DataSource=ds.Tables[0];
DataGrid1.DataBind();
}


protected void Button1_Click(object sender, System.EventArgs e)
{
Button3.Visible=false;
try

{
TextBox1.Enabled=true;
string strQry = "insert into emp values(" + int.Parse(TextBox1.Text.Trim()) + ",'" + TextBox2.Text.Trim() + "', '" + TextBox3.Text.Trim()+"')";
SqlCommand cmd=new SqlCommand(strQry,con);
int res= cmd.ExecuteNonQuery();
if (res > 0)
{
Label4.Text = "Record Inserted";
}
else
{
Label4.Text = "Record Insertion failed";
}
FillGrid(con);
TextBox1.Text="";
TextBox2.Text="";
TextBox3.Text="";
}
catch(Exception ex)
{
string str = ex.Message;
}
}

protected void Button3_Click(object sender, System.EventArgs e)
{

Button1.Visible=false;
TextBox1.Enabled=false;
string Qry="update emp set ename='"+TextBox2.Text+"', dept='"+TextBox3.Text+"' where eno=" + TextBox1.Text + "";
SqlCommand cmd=new SqlCommand(Qry,con);
int res= cmd.ExecuteNonQuery();
if (res > 0)
{
Label4.Text = "Record updated";
}
else
{
Label4.Text = "Record updation failed";
}
}


private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Label4.Text="";
Button3.Visible=true;
Button1.Visible=false;
TextBox1.Enabled=false;
if(e.CommandName.ToLower() == "edit")
{
if(e.Item.Cells[0].Text.Trim() != " ")
{
TextBox1.Text = e.Item.Cells[0].Text.Trim();
}
else
{
TextBox1.Text = "";
}
if(e.Item.Cells[1].Text.Trim() != " ")
{
TextBox2.Text = e.Item.Cells[1].Text.Trim();
}
else
{
TextBox2.Text = "";
}
if(e.Item.Cells[2].Text.Trim() != " ")
{
TextBox3.Text = e.Item.Cells[2].Text.Trim();
}
else
{
TextBox3.Text = "";
}
}
}

private void DatGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strQry="delete from emp where eno=" + e.Item.Cells[0].Text +"" ;
sqlcom = new SqlCommand(strQry,con);
int res = sqlcom.ExecuteNonQuery();
if (res > 0)
{
Label4.Text = "Record Deleted";
}
else
{
Label4.Text = "Record Deletion failed";
}
FillGrid(con);
Button1.Visible = true;
}


protected void Button4_Click(object sender, System.EventArgs e)
{
Label4.Text="";
TextBox1.Enabled=true;
TextBox1.Text="";
TextBox2.Text="";
TextBox3.Text="";
}


private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemIndex>-1)

{
LinkButton lnkbtnDelete = new LinkButton();
lnkbtnDelete = (LinkButton)e.Item.FindControl("lnkbtnDelete");
lnkbtnDelete.Attributes.Add("onclick","return confirm_entry('Are you sure to delete '+ \"'"+e.Item.Cells[0].Text.Trim()+"'\" );");
}
}


javascript:

//for confirmation to delete
function confirm_entry(path)
{
//alert("stop");
abc=confirm(path);
if (abc!=true)
{
return false;
}
}





Did you like this resource? Share it with your friends and show your love!


Responses to "Displaying,Adding,Updating,Editing,Deleting data of a Grid"
Author: ravi    01 Oct 2008Member Level: Gold   Points : 1

Hi ,

Plz dont consider Html Code becoz html code is not displaying
properly ie tags and some code is replaced by Empty space


Regards
Ravi



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Reading DataFrom XML file and displaying in the datagridview
    Previous Resource: Displaying Images in GridView
    Return to Resources
    Post New Resource
    Category: DataGridView


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Grid Operations  .  Adding  .  Data  .  Deleting  .  Editing  .  Updating  .  Grid  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.