A voting function module is indispensable for viewing voting results. Using a progress bar to display each voting result can provide a clear effect. The following is my method, please feel free to enlighten me:
1: Make a picture to use as a progress bar. You only need a very small picture, such as 20px high and 1px wide.
2: Insert an image control into the cell where the progress bar is to be displayed, and set its imageUrl to the location of the completed picture.
3: Use a dataReader object dr to save the number of votes taken out for each item, use an int type variable sum to save the total number of votes taken out, and define a double type variable for each item to save the result of dividing the number of individual votes by (/) the total number of votes ( decimal), and then define an int type variable to save the length of the progress bar to be displayed eventually (use the previous double type variable * the length of the cell used to display the progress bar, and then force conversion to int type), assign the length Just give the width attribute of the image. The following is my code snippet, which displays four progress bars:
SqlCommand cmd=new SqlCommand("select * from TvoteNum order by Vid",con);//SQL statement to find out the voting results of each item
SqlDataReader dr=cmd.ExecuteReader();
......
SqlCommand cmd1=new SqlCommand("select sum(Vnum) from TvoteNum",con1);//SQL statement to find out the total number of votes
int sum=Convert.ToInt32(cmd1.ExecuteScalar());
...
dr.Read();//Read the first record of the datareader object
this.Label1.Text=dr.GetInt32(1).ToString();//Number of votes for the first item
double w1=(Convert.ToDouble(this.Label1.Text)/sum);//The number of votes for this item accounts for the percentage of the total number of votes
int wid1=(int)(w1*310);//Convert to specific pixels, 310 is the length of the cell to be used to display the progress bar
this.Image1.Width=wid1;//The width assigned to the image
dr.Read();//Read the second record
this.Label2.Text=dr.GetInt32(1).ToString();
double w2=(Convert.ToDouble(this.Label2.Text)/sum);
int wid2=(int)(w2*310);
this.Image2.Width=wid2;
dr.Read();//Read the third record
this.Label3.Text=dr.GetInt32(1).ToString();
double w3=(Convert.ToDouble(this.Label3.Text)/sum);
int wid3=(int)(w3*310);
this.Image3.Width=wid3;
dr.Read();//Read the fourth record
this.Label4.Text=dr.GetInt32(1).ToString();
double w4=(Convert.ToDouble(this.Label4.Text)/sum);
int wid4=(int)(w4*310);
this.Image4.Width=wid4;
......