2015-05-02 89 views
2

我想顯示一些變量值gridview如何在gridview中綁定變量值?

代碼:

int cnt = Convert.ToInt32(txtCount.Text); 
DateTime start = Convert.ToDateTime(txtStart.Text); 
DateTime end = Convert.ToDateTime(txtEnd.Text); 
TimeSpan datedifference = end.Subtract(start); 
int dateCount = datedifference.Days; 
float maxUpload = dateCount * 288; 
float remainingUpload = maxUpload - cnt; 
float averageUpload = remainingUpload/(dateCount * 288) * 100; 

我想表明的值dateCountmaxUploadremainingUploadaverageUpload在GridView。請幫助我找到合適的解決方案。謝謝。

+0

所以你'Gridview'將只有1行? – Wanderer

+0

@Ullas:是的...... – Reshma

+0

@Reshma如果只有一行比爲什麼要使用gridview?只需要創建html表格並在html表格的td中放置四個標籤 – Sandeep

回答

1

綁定值添加您的變量爲multi-dimensional array
然後將該數組綁定到DataTable

代碼

int cnt = Convert.ToInt32(txtCount.Text); 
DateTime start = Convert.ToDateTime(txtStart.Text); 
DateTime end = Convert.ToDateTime(txtEnd.Text); 
TimeSpan datedifference = end.Subtract(start); 
int dateCount = datedifference.Days; 
float maxUpload = dateCount * 288; 
float remainingUpload = maxUpload - cnt; 
float averageUpload = remainingUpload/(dateCount * 288) * 100; 

string[,] row1 = { { dateCount.ToString(), maxUpload.ToString(), remainingUpload .ToString(), averageUpload .ToString() } }; 

DataTable table = new DataTable(); 
table.Columns.Add("DateCount", typeof(string)); 
table.Columns.Add("maxUpload", typeof(string)); 
table.Columns.Add("remainingUpload", typeof(string)); 
table.Columns.Add("averageUpload", typeof(string)); 
for (int i = 0; i <= row1.GetUpperBound(0); i++) 
{ 
    table.Rows.Add(); 
    table.Rows[i]["DateCount"] = row1[i, 0]; 
    table.Rows[i]["maxUpload"] = row1[i, 1]; 
    table.Rows[i]["remainingUpload"] = row1[i, 2]; 
    table.Rows[i]["averageUpload"] = row1[i, 3]; 
} 

gridview1.DataSource = table; 
gridview1.DataBind(); 

源代碼

<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="true"> 

</asp:GridView> 

而且不要忘了添加此namespace也。

using System.Data; 
1

嘗試這樣的:

<asp:TemplateField HeaderText="DateCount"> 
      <ItemTemplate> 
       <%# dateCount %> 
//Your Public Scope Variable Goes Here 
      </ItemTemplate> 
     </asp:TemplateField> 
1

首先創建一個數據表然後設置報頭添加到網格視圖然後使用gridview.Add方法值添加到網格view.then設置數據源到網格視圖在最後與GridView控件

  DataTable dt = new DataTable(); 
      dt.Columns.AddRange(new DataColumn[3] { new DataColumn("header1"), new DataColumn("header2 "), new DataColumn("header3") }); 
      dt.Rows.Add(cnt , start , end); 

      GridView1.DataSource = dt; 
      GridView1.DataBind(); 

How add values of columns of gridview in asp.net

1

如果您需要顯示只有一行比你可以使用標籤和HTML表:

<table> 
     <tr> 
      <th>Count</th> 
      <th>Max Upload</th> 
      <th>Remaining Upload</th> 
      <th>Average Upload</th> 
     </tr> 
     <tr> 
      <td> 
       <asp:Label ID="lblCount" runat="server" ></asp:Label> 
      </td> 
      <td> 
       <asp:Label ID="lblMaxUpload" runat="server" ></asp:Label> 
      </td> 
      <td> 
       <asp:Label ID="lblRemainingUpload" runat="server" ></asp:Label> 
      </td> 
      <td> 
       <asp:Label ID="lblAverageUpload" runat="server" ></asp:Label> 
      </td> 
     </tr> 
    </table> 

int cnt = Convert.ToInt32(txtCount.Text); 
DateTime start = Convert.ToDateTime(txtStart.Text); 
DateTime end = Convert.ToDateTime(txtEnd.Text); 
TimeSpan datedifference = end.Subtract(start); 
int dateCount = datedifference.Days; 
float maxUpload = dateCount * 288; 
float remainingUpload = maxUpload - cnt; 
float averageUpload = remainingUpload/(dateCount * 288) * 100; 

lblCount.Text = Convert.ToString(dateCount); 
lblMaxUpload.Text = Convert.ToString(maxUpload); 
lblRemainingUpload.Text = Convert.ToString(remainingUpload); 
lblAverageUpload.Text = Convert.ToString(averageUpload);