Windows 窗体 DataGridView 的 ItemDataBound?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/650271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ItemDataBound for a Windows Forms DataGridView?
提问by Agnel Kurian
I am binding an array to a DataGridView. I have a column containing file paths which maps to a column of push buttons. I would like to pre-process the file path so that the DataGridViewButtonColumn displays only the file name (sans the parent folders).
我将数组绑定到 DataGridView。我有一列包含映射到一列按钮的文件路径。我想预处理文件路径,以便 DataGridViewButtonColumn 只显示文件名(没有父文件夹)。
How do I intercept and pre-process values before they are bound to the columns of a DataGridView?
如何在将值绑定到 DataGridView 的列之前拦截和预处理值?
(I have seen something like this in ASP.NET data binding and was hoping I could do the same here.)
(我在 ASP.NET 数据绑定中看到过类似的东西,希望我能在这里做同样的事情。)
回答by GWLlosa
Handle the CellFormatting event. You're going to get a value passed in from the event args that represents the unformatted value. Take it and parse it (Path.GetFileName()) or something like that), and set the ConvertEventArgs.Value property to your new string. Set the "FormattingApplied" flag to true, to prevent further formatting events. That oughta cover you.
处理 CellFormatting 事件。您将获得一个从事件 args 传入的值,该值表示未格式化的值。获取并解析它(Path.GetFileName())或类似的东西),并将 ConvertEventArgs.Value 属性设置为您的新字符串。将“FormattingApplied”标志设置为 true,以防止进一步的格式化事件。那应该盖住你。