C#自定义格式设置一个DataGridView列的显示数据,而无需更改基础值自定义、格式、基础、数据

由网友(稳拿人心)分享简介:我有一个DataGridView被填充的对象的集合。第一列内的值是类似于:I have a datagridview being populated by a collection of objects.the values within the first column are similar to:SOME...

我有一个DataGridView被填充的对象的集合。 第一列内的值是类似于:

I have a datagridview being populated by a collection of objects. the values within the first column are similar to:

SOMEDISPLAYTEXT#T: blasndw lwwdjawn wjnawdja

"SOMEDISPLAYTEXT#T:blasndwlwwdjawnwjnawdja"

somedisplaytext#T: kndwla igrhysbv kjnfak

"somedisplaytext#T:kndwlaigrhysbvkjnfak"

我不希望要改变这些值,因为我也在不断地更新它们,但我想在DataGridView只显示该字符串somedisplaytext的第一部分,直到但不包括'#' ..无改变的基本价值。

I do not wish to wish to change these values, as i am also constantly updating them, however, i wish the datagridview to only show the first part of this string 'somedisplaytext', up to but not including the '#' ..without changing the underlying values.

推荐答案

如果您使用的WinForms:

If you use WinForms:

根据MSDN(的http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx ),你可以处理一个DataGridView的CellFormating事件,然后更改值被格式化的方式。

According to MSDN ( http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx ), you can handle the CellFormating event of a DataGridView and then change the way the value is formatted.

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    // If the column is the Artist column, check the
    // value.
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Artist")
    {
        if (e.Value != null)
        {
            // Check for the string "pink" in the cell.
            string stringValue = (string)e.Value;
            stringValue = stringValue.ToLower();
            if ((stringValue.IndexOf("pink") > -1))
            {
                e.CellStyle.BackColor = Color.Pink;
            }

        }
    }
    else if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Release Date")
    {
        ShortFormDateFormat(e);
    }
}

这第一种方法将改变背景颜色,如果列的艺术家包括粉红,并会改变的专栏发布日期的值与下面的方法的格式为:

This first method will change the background color if the column Artist contains "pink", and will change the format of the values in the column "Release Date" with the below method:

您可以在这里看到,你就必须为更换DataGridViewCellFormattingEventArgs的Value属性

You can see here that you just have to replace the Value property of the DataGridViewCellFormattingEventArgs

//Even though the date internaly stores the year as YYYY, using formatting, the
//UI can have the format in YY.  
private static void ShortFormDateFormat(DataGridViewCellFormattingEventArgs formatting)
{
    if (formatting.Value != null)
    {
        try
        {
            System.Text.StringBuilder dateString = new System.Text.StringBuilder();
            DateTime theDate = DateTime.Parse(formatting.Value.ToString());

            dateString.Append(theDate.Month);
            dateString.Append("/");
            dateString.Append(theDate.Day);
            dateString.Append("/");
            dateString.Append(theDate.Year.ToString().Substring(2));
            formatting.Value = dateString.ToString();
            formatting.FormattingApplied = true;
        }
        catch (FormatException)
        {
            // Set to false in case there are other handlers interested trying to
            // format this DataGridViewCellFormattingEventArgs instance.
            formatting.FormattingApplied = false;
        }
    }
}
阅读全文

相关推荐

最新文章