开发者

Adding color to a table cell using C#

开发者 https://www.devze.com 2023-03-22 08:15 出处:网络
I am creating a table and adding cells with contents through c sharp code. My code is as given below:

I am creating a table and adding cells with contents through c sharp code. My code is as given below:

//creating the table
Table table1 = new Table();
table1.ID = "table1";
table1.BorderStyle = BorderStyle.Dashed;
table1.GridLines = GridLines.Both;
this.Controls.Add(table1);

//adding first row
TableRow row1 = new TableRow();

//adding first cell
TableCell cell1 = new TableCell();

//adding label
Label text1 = new Label();
text1.Text = "Sourav Ganguly";

cell1.Controls.Add(text1);
row1.Controls.Add(cell1);
table1.Controls.Add(row1);

//adding second cell
TableCell cell2 = new TableCell();

//adding label
Label text2 = new Label();
text2.Text = "Rahul Dravid";

cell2.Controls.Add(text2);
row1.Controls.Add(cell2);

//adding third cell
TableCell cell3 = new TableCell();

//adding label
Label text3 = new Label();
text3.Text = "Sachin Tendulkar";

cell3.Controls.Add(text3);
row1.Controls.Add(cell3);

//adding second row
TableRow row2=new TableRow();

//adding first cell
TableCell cell4 = new TableCell();

//adding label
Label text4 = new Label();
text4.Text = "Virender Shewag";

cell4.Controls.Add(text4);
row2.Controls.Add(cell4);
table1.Controls.Add(row2);

//adding second cell
TableCell cell5 = new TableCell();

//adding label
Label text5 = new Label();
text5.Text = "MS Dhoni";

cell5.Controls.Add(text5);
row2.Controls.Add(cell5);
table1.Controls.Add(row2);

//adding third cell
TableCell cell6 = new TableCell();

//adding label
Label text6 = new Label();
text6.Text = "Zaheer Khan";

cell6.Controls.Add(text6);
row2.Controls.Add(cell6);
table1.Controls.Add(row2);

I wish to add a background color for each ce开发者_运维问答ll. Is it possible to create anything like this? i.e in the first cell of the first row, I wish to add red color to only about 50% of the cell. I wish the cell to remain colorless (usual white color) for the remaining 50%. Similarly, for the second cell of the first row, I wish to add yellow color for 80% of the cell and I want the remaining 20% of the cell to be in default white color. Is is possible to achieve such a kind of functionality functionality using C#?


Not exactly, but you can try to replicate the behavior by setting your Text property to the following:

<div style="width:100px;height:20px">
  <div style="background-color:red;width:80%;height:20px"/>
  <div style="float:left">Hello</div>
</div>

Of course, you'll want to replace the hard-coded dimensions and color.


If your % are large, you can use colspan property for the cell and extend a particular colour to represent your graph.

Another option is to include a 1px image and then stretch it as required.

Something like:

TableCell cell= new TableCell();
Image img = new Image();
img.ImageUrl = "image url";
img.Width=80; //this can represent 80%
cell.Controls.Add(img);


This is NOT optimized to render efficiently, but the principle works (CSS3):

//adding first row
TableRow row1 = new TableRow();

//adding first cell
TableCell cell1 = new TableCell();

//adding label
Label text1 = new Label();
text1.Text = "Sourav Ganguly";

//adding color
Integer perShaded = NUM;
cell1.Style = "background-image: url('color.gif'); background-size: " + perShaded + "% 100%;";

cell1.Controls.Add(text1);
row1.Controls.Add(cell1);
table1.Controls.Add(row1);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号