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 » ASP.NET GridView »

Adding rows to grid view at runtime


Posted Date: 07 Nov 2009    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: krishnavenikaladiMember Level: Gold    
Rating: 1 out of 5Points: 5



The following snippet demonstrates adding of row to grid view programmatically at runtime.
Name spaces to used are:
using System.Data.OracleClient;
using System.Data;

Step1:

The following code should be coded under page load event


OracleConnection cn = new OracleConnection("user id=scott;password=tiger;data source=ganesh1");
OracleDataAdapter da = new OracleDataAdapter("select * from emp", cn);
ds = new DataSet();
da.Fill(ds, "temp");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();


Step2:

The following code should be coded under row created event of grid view




protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{ //checking whether rowtype is datacontrolrowtype
if (e.Row.RowType == DataControlRowType.DataRow)
{
double annsal;
//getting the required data from database
annsal =double .Parse ( ds.Tables[0].Rows[e.Row.RowIndex]["sal"].ToString()) * 12;
//initializing and adding label control to gridview

Label l1=new Label();

l1.Text =annsal .ToString ();
e.Row .Cells [1].Controls .Add (l1);

//checking tha annsal and drawing the row with particular color

if (annsal > 1000)
{
e.Row.BackColor = System.Drawing.Color.Red;

}
else
e.Row.BackColor = System.Drawing.Color.Gray;
}
}



Step 3: Customize the gridview.Use 2 templates
Template 1-->
A) Header Text -->Employee Name

B) Place Label in Item Template and bind Text to Eval("Ename")
Template2-->
A) Header Text=Annual Salary

Set Auto Generate fields to False

Step 4: Execute

Explanation
-->The grid view is binded with the database
-->using row created event of grid view add the row to grid view at runtime





Responses

Author: ABitSmart    07 Nov 2009Member Level: Diamond   Points : 0
Format HTML of your resource


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Rowcreated  .  

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: Delete selected rows in different pages of gridview control using Asp.net
Previous Resource: Custom Paging for all data controls
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use