Reputation: 1430
I want to display some variable value in gridview.
Code:
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;
I want show the value of dateCount, maxUpload,remainingUpload and averageUpload in a gridview. Please help me to find a proper solution. Thank you.
Upvotes: 4
Views: 5791
Reputation: 11566
Add your variables to a multi-dimensional array
.
And then bind that array to a DataTable
.
Code
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();
Source Code
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="true">
</asp:GridView>
And don't forget to add this namespace
also.
using System.Data;
Upvotes: 4
Reputation: 1210
If you need to display only one row than you can use label and html table:
<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 );
Upvotes: 1
Reputation: 3192
First create a data table then set header to the grid view then using gridview.Add method add values to grid view.then set the data source to grid-view at last bind values with 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
Upvotes: 1
Reputation: 10295
try like this:
<asp:TemplateField HeaderText="DateCount">
<ItemTemplate>
<%# dateCount %>
//Your Public Scope Variable Goes Here
</ItemTemplate>
</asp:TemplateField>
Upvotes: 1