Ema112 Ответов: 1

Как установить ширину HTML, созданного из datatable в ASP.NET с#?


Я хочу установить ширину заголовка таблицы для html, созданного из таблицы данных в asp.net с#?


Мой код выглядит следующим образом:
private string CreateTableHTML(DataTable DT)
        {
            string result = "";
            GridView gv = new GridView();
            gv.DataSource = DT;
            gv.DataBind();
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            result = htw.InnerWriter.ToString();

            return result;
        }

Currently html generated is as below:
<div>
	<table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">
		<tr>
			<th scope="col">DATE</th><th scope="col">DESCRIPTION</th><th scope="col">VALUE DATE</th><th scope="col">DEBIT</th><th scope="col">CREDIT</th><th scope="col">BALANCE</th>
		</tr><tr>
			<td>04/01/2011</td><td>OPENING BALANCE</td><td>04/01/2011</td><td> </td><td> </td><td>0.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TEST Test Descrption added by jebin</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>PLATINUM STATEMENT TEMPLATE MISCELLANEOUS TEST Description for the 000018473018 account number </td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>MISCELLANEOUS TESTMISCELLANEOUS TESTMISCELLANEOUS TESTMISCELLANEOUS TESTMISCELLANEOUS TESTMISCELLANEOUS TESTMISCELLANEOUS TESTMISCELLANEOUS TESTMISCELLANEOUS TEST</td><td>04/01/2011</td><td>-</td><td>1,500.00 </td><td>1,500.00 </td>
		</tr><tr>
			<td>04/01/2011</td><td>CLOSING BALANCE</td><td>04/01/2011</td><td> </td><td> </td><td>1,500.00 </td>
		</tr><tr>
			<td>0   Debits</td><td>0.00</td><td> </td><td> </td><td> </td><td> </td>
		</tr><tr>
			<td>40   Credits</td><td>60,000.00</td><td> </td><td> </td><td> </td><td> </td>
		</tr><tr>
			<td>Available Balance:</td><td>1,500.00</td><td> </td><td> </td><td> </td><td> </td>
		</tr>
	</table>
</div>

I want to set table header th with width as desired.

What I have tried:

private string CreateTableHTML(DataTable DT)
        {
            string result = "";
            GridView gv = new GridView();
            gv.DataSource = DT;
            gv.DataBind();
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            result = htw.InnerWriter.ToString();

            return result;
        }

Herman&lt;T&gt;.Instance

Вы должны настроить это в своем элементе управления GridView.

1 Ответов

Рейтинг:
0

Karthik_Mahalingam

попробуй вот так

private string CreateTableHTML(DataTable DT, Unit width)
      {
          string result = "";
          GridView gv = new GridView();
          gv.Width = width;
          gv.DataSource = DT;


var table = CreateTableHTML(dt,500);


Это приведет к созданию стиля шириной 500 пикселей
<div>
	<table cellspacing="0" rules="all" border="1" style="width:500px;border-collapse:collapse;">