Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C向表格单元格添加颜色#_C#_.net_Asp.net_Html - Fatal编程技术网

C# 使用C向表格单元格添加颜色#

C# 使用C向表格单元格添加颜色#,c#,.net,asp.net,html,C#,.net,Asp.net,Html,我正在创建一个表,并通过c代码添加包含内容的单元格。我的代码如下所示: //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

我正在创建一个表,并通过c代码添加包含内容的单元格。我的代码如下所示:

//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、 e在第一行的第一个单元格中,我只希望在大约50%的单元格中添加红色。我希望电池在剩下的50%中保持无色(通常为白色)。类似地,对于第一行的第二个单元格,我希望为80%的单元格添加黄色,并希望其余20%的单元格为默认白色。使用C#?

实现此类功能并非完全可行,但您可以尝试通过将文本属性设置为以下值来复制该行为:

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

你好

当然,您需要替换硬编码的尺寸和颜色。

如果百分比较大,可以对单元格使用colspan属性,并扩展特定颜色以表示图形

另一种选择是包括1px图像,然后根据需要拉伸它

比如:

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

这不是为了高效渲染而优化的,但其原理是有效的(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);