Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将hhmm DataGridView单元格值转换为TimeSpan字段_C#_.net_Datagridview - Fatal编程技术网

C# 将hhmm DataGridView单元格值转换为TimeSpan字段

C# 将hhmm DataGridView单元格值转换为TimeSpan字段,c#,.net,datagridview,C#,.net,Datagridview,我想在DataGridView列中将TimeSpan字段显示为hhmm。并允许用户以这种格式进行编辑。据我所知,我需要为单元格式化、单元解析和单元验证事件添加一些逻辑。因此,我想我必须检查列名,并为那些需要这个列名的人处理它 但是,为了代码重用,我还可以如何更好地解决这个问题呢?我是否可以创建一个自定义DataGridViewColumn类,将此逻辑放在其中?如何实现这一目标?我看不到DataGridViewColumn类存在任何事件,因此不确定在这里要做什么 我将查看DataGridViewC

我想在DataGridView列中将TimeSpan字段显示为hhmm。并允许用户以这种格式进行编辑。据我所知,我需要为单元格式化、单元解析单元验证事件添加一些逻辑。因此,我想我必须检查列名,并为那些需要这个列名的人处理它


但是,为了代码重用,我还可以如何更好地解决这个问题呢?我是否可以创建一个自定义DataGridViewColumn类,将此逻辑放在其中?如何实现这一目标?我看不到DataGridViewColumn类存在任何事件,因此不确定在这里要做什么

我将查看
DataGridViewColumn.CellTemplate
属性,它属于以下类型:

public abstract class DataGridViewCell : DataGridViewElement, ICloneable, IDisposable
它具有以下有趣的特性:

Value: object
ValueType: Type
ValueTypeConverter: TypeConverter
从这里,我将看到
TypeConverter


希望这有帮助,这是我在大约2分钟的浏览中可以收集到的。

也许对你来说太晚了,但我想这会帮助其他人。昨天我几乎遇到了同样的问题。 我通过为我的TimeSpan成员创建类包装器解决了这个问题,在这里我重写了ToString方法(以便以首选格式显示时间),并创建了Parse(String)方法,当用户完成单元格编辑时,该方法会自动调用。最后,为了捕获可能在Parse方法中生成的异常,为DataGridView的DataError事件创建处理程序。 例如:

class TimeSpanDecorator
{
受保护的时间跨度时间跨度;
公共TimeSpanDecorator(TimeSpan ts)
{
timeSpan=ts;
}
公共重写字符串ToString()//返回所需的TimeSpan视图
{
返回timeSpan.Hours+“:”+timeSpan.Minutes;
}
public static TimeSpanDecorator Parse(字符串值)//以任何方式解析输入的值
{
String[]parts=value.Split(“:”);
如果(parts.Length!=2)
抛出新ArgumentException(“错误格式”);
inthours=Int32.Parse(parts[0]);
int minutes=Int32.Parse(parts[1]);
TimeSpanDecorator结果=新TimeSpanDecorator(新TimeSpan(小时,分钟,0));
如果(result.timeSpan.Ticks<0)
抛出新ArgumentException(“您应该提供正时间值”);
返回结果;
}
//其他成员
}
内部部分类MainForm:Form
{
(...)
私有void dataGridView_DataError(对象发送方,DataGridViewDataErrorEventArgs e)
{
MessageBox.Show(“发生错误:+e.Exception.Message,“警告!”);//显示生成的参数异常
e、 ThroweException=false;//告诉表单我们已经处理了错误
}
}

希望这对任何人都有帮助。

您如何将数据绑定到网格?
class TimeSpanDecorator
{
    protected TimeSpan timeSpan;
    public TimeSpanDecorator(TimeSpan ts)
    {
        timeSpan = ts;
    }
    public override string ToString() // return required TimeSpan view
    {
        return timeSpan.Hours + ":" + timeSpan.Minutes;
    }
    public static TimeSpanDecorator Parse(String value) // parse entered value in any way you want
    {
        String[] parts = value.Split(':');
        if (parts.Length != 2)
            throw new ArgumentException("Wrong format");
        int hours = Int32.Parse(parts[0]);
        int minutes = Int32.Parse(parts[1]);
        TimeSpanDecorator result = new TimeSpanDecorator(new TimeSpan(hours, minutes, 0));
        if (result.timeSpan.Ticks < 0)
            throw new ArgumentException("You should provide positive time value");
        return result;
    }
    //other members
}

internal partial class MainForm : Form
{
    (...)
    private void dataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        MessageBox.Show("Error occured: " + e.Exception.Message, "Warning!"); // showing generated argument exception
        e.ThrowException = false; // telling form that we have processed the error
    }
}