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 » C# Syntax »

Creating Datagrid


Posted Date: 19 Sep 2007    Resource Type: Code Snippets    Category: C# Syntax
Author: SantroMember Level: Bronze    
Rating: 1 out of 5Points: 10



This code sample has been given to devlope datagrid which can be used to access data into it.



public partial class pgDatagrid1 : System.Web.UI.Page
{
string strConnectionstring;
clsBindgrid1 objGrid1 = new clsBindgrid1();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ds = objGrid1.bindgrid1();
dgTest1.DataSource = ds;
dgTest1.DataBind();
}
}

protected void dgTest1_CancelCommand(object source, DataGridCommandEventArgs e)
{
dgTest1.EditItemIndex = -1;
ds = objGrid1.bindgrid1();
dgTest1.DataSource = ds;
dgTest1.DataBind();
}
protected void dgTest1_EditCommand(object source, DataGridCommandEventArgs e)
{
dgTest1.EditItemIndex = int.Parse(e.Item.ItemIndex.ToString());
ds = objGrid1.bindgrid1();
dgTest1.DataSource = ds;
dgTest1.DataBind();
}
protected void dgTest1_DeleteCommand(object source, DataGridCommandEventArgs e)
{

if (e.CommandName.ToString() == "delete")
{
int intBookid = int.Parse(e.Item.Cells[0].Text.ToString());

SqlConnection conDelete;
strConnectionstring = ConfigurationManager.AppSettings["strConstr"];
conDelete = new SqlConnection(strConnectionstring);
conDelete.Open();
string strQuery;
strQuery = "DELETE FROM BOOK WHERE BOOKID=" + intBookid;
SqlCommand cmdDelete = new SqlCommand();
cmdDelete.CommandText = strQuery;
cmdDelete.Connection = conDelete;
cmdDelete.CommandType = CommandType.Text;
cmdDelete.ExecuteNonQuery();
conDelete.Close();
dgTest1.EditItemIndex = int.Parse(e.Item.Cells[0].Text.ToString());
ds = objGrid1.bindgrid1();
dgTest1.DataSource = ds;
dgTest1.DataBind();
}
}
protected void btnInsert_Click(object sender, EventArgs e)
{

SqlConnection conInsert;
strConnectionstring = ConfigurationManager.AppSettings["strConstr"];
conInsert = new SqlConnection(strConnectionstring);
conInsert.Open();
string strInsquery;
strInsquery = "INSERT INTO BOOK(BOOKNAME,BOOKPRICE)VALUES('" + tbBookname.Text.ToString() + "'," + int.Parse(tbBookprice.Text.ToString()) + ")";
SqlCommand cmdInsert = new SqlCommand();
cmdInsert.CommandText = strInsquery;
cmdInsert.Connection = conInsert;
cmdInsert.CommandType = CommandType.Text;
cmdInsert.ExecuteNonQuery();
conInsert.Close();
dgTest1.EditItemIndex = -1;
ds = objGrid1.bindgrid1();
dgTest1.DataSource = ds;
dgTest1.DataBind();

}
protected void dgTest1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
int intBookid;
string strBookname;
int intBookprice;

intBookid = int.Parse(e.Item.Cells[0].Text.ToString());
strBookname = ((TextBox)e.Item.Cells[1].FindControl("txtbookname")).Text.ToString();
intBookprice = int.Parse(((TextBox)e.Item.Cells[2].FindControl("txtbookprice")).Text.ToString());

SqlConnection conUpdate;
strConnectionstring = ConfigurationManager.AppSettings["strConstr"];
conUpdate = new SqlConnection(strConnectionstring);
conUpdate.Open();
string strUpdatequery;
strUpdatequery = "UPDATE BOOK SET BOOKNAME='" + strBookname + "',BOOKPRICE=" + intBookprice + " WHERE BOOKID=" + intBookid;
SqlCommand cmdUpdate = new SqlCommand();
cmdUpdate.CommandText = strUpdatequery;
cmdUpdate.Connection = conUpdate;
cmdUpdate.CommandType = CommandType.Text;
cmdUpdate.ExecuteNonQuery();
dgTest1.EditItemIndex = -1;
ds = objGrid1.bindgrid1();
dgTest1.DataSource = ds;
dgTest1.DataBind();
}
}









Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

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: Code for Enhancing the Search of Items in a Combo box:
Previous Resource: Inserting data in grid
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use