Thursday, January 24, 2013

How to Bind DataTable to ASP.NET Pie Chart


To Bind DataTable to ASP.NET Pie Chart, refer the below code.
 DataTable dt=new DataTable();  
   
 dt.Columns.Add("Name");  
 dt.Columns.Add("Percentage");  
   
 DataRow dr=dt.NewRow();  
 dr["Name"]="Name1";  
 dr["Percentage"]="80";  
 dt.Rows.Add(dr);  
   
 DataRow dr1=dt.NewRow();  
 dr1["Name"]="Name2";  
 dr1["Percentage"]="75";  
 dt.Rows.Add(dr1);  
   
 DataRow dr2=dt.NewRow();  
 dr2["Name"]="Name3";  
 dr2["Percentage"]="90";  
 dt.Rows.Add(dr2);  
   
 DataRow dr3=dt.NewRow();  
 dr3["Name"]="Name4";  
 dr3["Percentage"]="100";  
 dt.Rows.Add(dr3); 
   
 Dictionary<string, int> chartData = new Dictionary<string, int>();  
 foreach (DataRow r in dt.Rows)  
 {  
     string key = r["Name"].ToString();  
     int value = Convert.ToInt32(r["Percentage"]);  
     chartData.Add(key, value);  
 }  
   
 Chart1.Series["Series1"].Points.DataBind(chartData , "Key", "Value", string.Empty);  

No comments:

Post a Comment