gpt4 book ai didi

c# - 如何在gridview中绑定(bind)变量值?

转载 作者:太空狗 更新时间:2023-10-30 00:51:04 24 4
gpt4 key购买 nike

我想在 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;

我想在 GridView 中显示 dateCountma​​xUploadremainingUploadaverageUpload 的值。请帮助我找到合适的解决方案。谢谢。

最佳答案

将变量添加到多维数组
然后将该数组绑定(bind)到 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>

并且不要忘记也添加这个命名空间

using System.Data;

关于c# - 如何在gridview中绑定(bind)变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29998412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com