C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » DataGridView »

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


Posted Date: 01 Oct 2008    Resource Type: Code Snippets    Category: DataGridView
Author: raviMember Level: Gold    
Rating: 1 out of 5Points: 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;
}
}




Responses

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      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Updating  .  Grid Operations  .  Grid  .  Editing  .  Deleting  .  Data  .  Adding  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Reading DataFrom XML file and displaying in the datagridview
Previous Resource: Displaying Images in GridView
Return to Discussion Resource Index
Post New Resource
Category: DataGridView


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use