C# 如何更改列值以在bind DataGridView中显示不同的值?
我有一个C# 如何更改列值以在bind DataGridView中显示不同的值?,c#,winforms,list,data-binding,datagridview,C#,Winforms,List,Data Binding,Datagridview,我有一个dataGridView,它是绑定到一些通用列表的数据(而T是一些具有属性的自定义类)。 问题在于,其中一个属性的类型为integer,它表示分钟数。 将列表绑定到dataGridView后,我希望该列显示小时,而不是默认的分钟 如何改变某个列的行为,在其上使用一些数学来显示一些不同的值 我必须在DataGridView.CellFormating事件中执行此操作吗 您有两个选项,第一个选项是首先操作通用列表,这应该比使用第二个选项更快,在每个行数据绑定事件上迭代列表 使用行数据绑定事
dataGridView
,它是绑定到一些通用列表的数据(而T是一些具有属性的自定义类)。
问题在于,其中一个属性的类型为integer
,它表示分钟数。
将列表绑定到dataGridView后,我希望该列显示小时,而不是默认的分钟
如何改变某个列的行为,在其上使用一些数学来显示一些不同的值
我必须在DataGridView.CellFormating
事件中执行此操作吗 您有两个选项,第一个选项是首先操作通用列表
,这应该比使用第二个选项更快,在每个行数据绑定事件
上迭代列表
- 使用
行数据绑定
事件
受保护的void gridview1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
int minutes=int.Parse(e.Row.Cells[YourColumnIndex].Text);
十进制小时=分钟/60;
e、 Row.Cells[YourColumnIndex].Text=hours.ToString();
}
}
- 使用
Evel
表达式
ASPX页
<asp:TemplateField HeaderText="Time">
<ItemTemplate>
<%# ConvertToHours(Eval("Minutes"))%>
</ItemTemplate>
</asp:TemplateField>
另一种方法。-一次性完成所有操作
<asp:TemplateField HeaderText="Time">
<ItemTemplate>
<asp:Label ID="lblTime" runat="server" Text='<%# Convert.ToInt32(Eval("Time")) Convert.ToInt32("60")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
我正在使用windows窗体。@MitjaBonca-ops!然后检查更新的答案。
<asp:TemplateField HeaderText="Time">
<ItemTemplate>
<asp:Label ID="lblTime" runat="server" Text='<%# Convert.ToInt32(Eval("Time")) Convert.ToInt32("60")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
// If the column is the Time column, check the
// value.
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Time")
{
if (e.Value != null)
{
//Your implementation.
}
}
}