0

So I have a table and within each cell, is a gridview. Basically it's tables within a table. Right now, the columns in each gridview have arbitrary widths, depending on the length of each text in each cell. I want to make them all the same so it's nicer to look at.

In a loop, I make each gridview, bind to a cell, bind to a row, and appending to the outer table. I tried to add this statement after I bound the gridview to a dataview to make the first columns consistent in width:

gvTemp.Columns[0].ItemStyle.Width = Unit.Pixel(400);

But I get an error saying that 0 was too big of an index, even though the gridview is not empty.

Am I using the wrong methods? Am I placing the code in an incorrect location?

4

2 回答 2

0

看起来 Columns 属性用于显式声明的列,您的列可能是数据源中隐含的。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.columns(v=vs.110).aspx

检查行属性。您可能必须设置第一行中单元格的宽度:

Rows[0].Cells[0].Width = Unit.Pixel(400)
于 2014-03-05T22:26:17.823 回答
0

解决这个问题的一种方法是 CSS 中的使用百分比与 javascript 中的一些简单数学相结合。使用 javascript 获取表中的列数,除以 100 并将结果作为宽度值应用到每列。这样,无论您有多少列或多少列,它总是会均匀地分割它们。

当您有很多列并且它们变得太窄而无法合理使用时,这会成为一个问题,在这种情况下,您最好为每列设置一个以像素为单位的宽度。

于 2014-03-05T22:16:22.777 回答