C# custom formatting a datagridview column's d

2019-07-11 18:41发布

问题:

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:\kndwla\igrhysbv\kjnfak"

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.

回答1:

If you use WinForms:

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:

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;
        }
    }
}


回答2:

One way could be to create a property in your class which returns the formatted Text

public PropertyForDisplay
{
    get
    {
       String[] array = OriginalProperty.Split('#');
       if(array.Length > 0)
           return array[0] ;

       return String.Empty;
    }
}