.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

gridview all operation

Posted By: pooja     Posted Date: March 19, 2010    Points:2   Category :ASP.Net
Hi frnfs...


please tell me all the operations in gridview using code behind... not sql data souce...

in C#


thanx in advabnce

Responses
Author: Amit Mehra             
Posted Date: March 20, 2010     Points: 5   

Hi,

if you would like to Add, edit, Delete records from gridview you can refer this article

http://www.dotnetspark.com/kb/643-how-to-editupdatedelete-gridview.aspx

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: Lalij Mer             
Posted Date: March 20, 2010     Points: 5   

Show here good code sample will help
protected void Page_Load(object sender, EventArgs e)

{



if (!Page.IsPostBack)

{

bindgrid();

total = 0;



}



}



public void bindgrid()

{

SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");

SqlCommand cmd = new SqlCommand("select CategoryName,CategoryID from Categories ", conn);



SqlDataAdapter da = new SqlDataAdapter("", conn);

da.SelectCommand = new SqlCommand("select CategoryName,CategoryID from Categories", conn);

DataSet ds = new DataSet();

da.Fill(ds, "data");

gdview.DataSource = ds.Tables[0].DefaultView;

gdview.DataBind();





}

protected void gdview_RowEditing(object sender, GridViewEditEventArgs e)

{



gdview.EditIndex = e.NewEditIndex;



bindgrid();



}

protected void gdview_RowUpdating(object sender, GridViewUpdateEventArgs e)

{



int catid = int.Parse(gdview.DataKeys[e.RowIndex].Value.ToString());

string strcatname=((TextBox)gdview.Rows[e.RowIndex].Cells[0].Controls[0]).Text;

SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");

SqlDataAdapter da = new SqlDataAdapter("", conn);

conn.Open();

da.UpdateCommand = new SqlCommand("update Categories set CategoryName='" + strcatname + "' where CategoryID=" + catid, conn);

da.UpdateCommand.ExecuteNonQuery();

conn.Close();

gdview.EditIndex = -1;

bindgrid();





}

protected void gdview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

{

gdview.EditIndex = -1;

bindgrid();

}

protected void gdview_RowDeleting(object sender, GridViewDeleteEventArgs e)

{



int catid = int.Parse(gdview.DataKeys[0].Value.ToString());

SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");

SqlDataAdapter da = new SqlDataAdapter("", conn);

conn.Open();

da.DeleteCommand = new SqlCommand("delete from Categories where CategoryID="+catid, conn);

da.DeleteCommand.ExecuteNonQuery();

conn.Close();

bindgrid();

}





Here also good article will help
http://www.aspdotnetcodes.com/GridView_Insert_Edit_Update_Delete.aspx

Please Check my answer if you help....
Thank You...

Lalit.
Author: susant             
Posted Date: March 20, 2010     Points: 5   

hi just check this link

http://highoncoding.com/

Author: Sagar Pardeshi             
Posted Date: April 07, 2010     Points: 5   

Hi

check following
links

http://aspalliance.com/1125 


Author: TotalDotNet             
Posted Date: August 16, 2010     Points: 5   

The following code will definitely help u. Also see the demo
http://www.totaldotnet.com/Data%20%20Controls/Property_DataControl_Demo/Prop_GridViewRowProperties.aspx




.cs file

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridBind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
gridBind();
}

private void gridBind()
{
try
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DotNetLearnningConnectionString"].ConnectionString))
{
using (SqlCommand sqcmd = new SqlCommand())
{
sqcmd.Connection = conn;
sqcmd.CommandText = "Select * from [City]";
sqcmd.CommandType = CommandType.Text;
using (SqlDataAdapter ad = new SqlDataAdapter(sqcmd))
{
using (DataSet das = new DataSet())
{
ad.Fill(das);
GridView1.DataSource = das;
GridView1.DataBind();
}
}
}
}

}

catch
{
throw;
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
DropDownList ddl1 = (DropDownList)e.Row.FindControl("DropDownList2");

using (DotNetLearnClassesDataContext dc = new DotNetLearnClassesDataContext())
{
var countries = from sa in dc.Countries
select new
{
sa.Country_Id,
sa.CountryName
};

var continents = from sa in dc.Continents
select new
{
sa.Continent_Id,
sa.Continent_Name

};
ddl.DataSource = countries;
ddl.DataTextField = "Country_Id";
ddl.DataValueField = "Country_Id";
ddl.DataBind();

ddl1.DataSource = continents;
ddl1.DataTextField = "Continent_Id";
ddl1.DataValueField = "Continent_Id";
ddl1.DataBind();
}
}
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow gvr=GridView1.Rows[GridView1.EditIndex];
DropDownList dd1 = (DropDownList)gvr.Cells[2].FindControl("DropDownList1");
DropDownList dd2 = (DropDownList)gvr.Cells[3].FindControl("DropDownList2");
using (DotNetLearnClassesDataContext dc = new DotNetLearnClassesDataContext())
{
TextBox id = (TextBox)gvr.Cells[0].Controls[0];
var data = dc.Cities.Single(m => m.City_Id == Convert.ToInt32(id.Text));
data.Continent_Id = Convert.ToInt32(dd2.SelectedValue);
data.Country_Id = Convert.ToInt32(dd1.SelectedValue);
dc.SubmitChanges();
GridView1.EditIndex = -1;
GridView1.DataSource = dc.Cities;
GridView1.DataBind();
}
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}

aspx
<form id="form1" runat="server">
<div>
<center>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="GridView1_RowEditing"
OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating"

onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="City_Id" />
<asp:BoundField DataField="City_Name" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Country_Id") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Continent">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Continent_Id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="Button2" runat="server" Text="Update" CommandName="Update" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</center>
</div>
</form>




Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend