Click here to Skip to main content
15,903,012 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to bookmark in ritchtextbox Pin
Pawel Gielmuda5-Jan-10 0:16
Pawel Gielmuda5-Jan-10 0:16 
AnswerRe: How to bookmark in ritchtextbox Pin
Eddy Vluggen5-Jan-10 10:29
professionalEddy Vluggen5-Jan-10 10:29 
QuestionWhat is the difference between Activator.CreateInstance and new operator ? Pin
md_azy4-Jan-10 17:45
md_azy4-Jan-10 17:45 
AnswerRe: What is the difference between Activator.CreateInstance and new operator ? Pin
_Maxxx_4-Jan-10 17:52
professional_Maxxx_4-Jan-10 17:52 
QuestionPrint Fit to Page Image Pin
rina184-Jan-10 15:45
rina184-Jan-10 15:45 
AnswerRe: Print Fit to Page Image Pin
AspDotNetDev4-Jan-10 17:56
protectorAspDotNetDev4-Jan-10 17:56 
QuestionMulti-color text in single cell of datagridview in C# Pin
auaryamane0074-Jan-10 15:21
auaryamane0074-Jan-10 15:21 
AnswerRe: Multi-color text in single cell of datagridview in C# [modified] Pin
petercrab5-Jan-10 0:11
petercrab5-Jan-10 0:11 
Hi,

You need to override the paint method for the cell object. Cells that contain just text are instances of DataGridViewTextBoxCell Class, so override that. Below is my attempt at a new class with over-ridden paint method. The draw method also only draws what will fit in cell so there will be no overlapping of data between cells. I have tried to make it as reusable as possible so you can define what colour you wish to change to and the number of consecutive numbers that should have colour changed. I also include code I used to test the class out and a couple of screenshots showing it working.

public class MultiColourDataGridViewTextBoxCell : DataGridViewTextBoxCell
    {
        public MultiColourDataGridViewTextBoxCell():base()
        {
        }

        /// <summary>
        /// overide paint method to colour text dependant on number of consecutive numbers
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="clipBounds"></param>
        /// <param name="cellBounds"></param>
        /// <param name="rowIndex"></param>
        /// <param name="cellState"></param>
        /// <param name="value"></param>
        /// <param name="formattedValue"></param>
        /// <param name="errorText"></param>
        /// <param name="cellStyle"></param>
        /// <param name="advancedBorderStyle"></param>
        /// <param name="paintParts"></param>
        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            int fontSize = 9;
            Brush textBrush = Brushes.Green;
            Font font = new Font(FontFamily.GenericSansSerif,fontSize,FontStyle.Regular);
            String stringPrint = (String)formattedValue;

            graphics.FillRectangles(Brushes.White,new Rectangle[]{cellBounds});
            StringPrintInfo[] stringPrintInfo = GetConsectiveNumbers(4, stringPrint, Brushes.Black, Brushes.Red);
            PrintConsectiveNumbers(stringPrintInfo,graphics, font,textBrush,cellBounds,StringFormat.GenericDefault);
            this.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);

        }

        /// <summary>
        /// from input string finds consective numbers marjs them in specified colour.
        /// </summary>
        /// <param name="numberConsecutive">number of consecutive numbers to look for</param>
        /// <param name="input">input string</param>
        /// <param name="bOriginal">default brush colour</param>
        /// <param name="bConsecutive">brush for consecutive numbers</param>
        /// <returns> an ordered array of text and colour pairs</returns>
        public StringPrintInfo[] GetConsectiveNumbers(int numberConsecutive, String input, Brush bOriginal, Brush bConsecutive)
        {
            List<StringPrintInfo> formatInformation = new List<StringPrintInfo>();
            int inputCount = 0; //how much of input string is used
            StringPrintInfo tmpStrPrntInfo;
            String regExp = "\\b[0-9]{4}\\b";

            Regex exp = new Regex(regExp);
            foreach (Match match in exp.Matches(input))
            {

                if (inputCount < match.Index)
                {
                    tmpStrPrntInfo = new StringPrintInfo();
                    String strTmp = input.Substring(0, match.Index);
                    tmpStrPrntInfo.str = strTmp;
                    tmpStrPrntInfo.brush = bOriginal;
                    inputCount = match.Index;
                    formatInformation.Add(tmpStrPrntInfo);
                }

                tmpStrPrntInfo = new StringPrintInfo();
                tmpStrPrntInfo.str = match.Value;
                tmpStrPrntInfo.brush = bConsecutive;
                inputCount = match.Index + match.Length;
                formatInformation.Add(tmpStrPrntInfo);
            }

            if (inputCount < input.Length)
            {
                tmpStrPrntInfo = new StringPrintInfo();
                String strTmp = input.Substring(inputCount);
                tmpStrPrntInfo.str = strTmp;
                tmpStrPrntInfo.brush = bOriginal;
                inputCount = input.Length;
                formatInformation.Add(tmpStrPrntInfo);
            }

            return formatInformation.ToArray();
        }

        /// <summary>
        /// Prints the cell with multicolour text. should be called as part of paint method
        /// </summary>
        /// <param name="toPrint"></param>
        /// <param name="graphics"></param>
        /// <param name="font"></param>
        /// <param name="textBrush"></param>
        /// <param name="cellBounds"></param>
        /// <param name="stringFormat"></param>
        public void PrintConsectiveNumbers(StringPrintInfo[] toPrint,Graphics graphics, Font font ,Brush textBrush,Rectangle cellBounds, StringFormat stringFormat)
        {
            float xOffset = 0;

            for(int i = 0;i<toPrint.Length;i++)
            {

                if (i > 0)
                {
                    SizeF stringSize = graphics.MeasureString(toPrint[i-1].str, font);
                    xOffset += stringSize.Width;
                }

                /*dont print of edges of cell*/
                if (cellBounds.X+ xOffset + graphics.MeasureString(toPrint[i].str, font).Width > cellBounds.X + cellBounds.Width)
                {
                    string cutDown = toPrint[i].str;

                    for (int j = cutDown.Length; j > 0; j--)
                    {
                        cutDown = cutDown.Substring(0, j);
                        if (cellBounds.X + xOffset + graphics.MeasureString(cutDown, font).Width <= cellBounds.X + cellBounds.Width)
                        {
                            graphics.DrawString(cutDown, font, toPrint[i].brush, cellBounds.X + xOffset, cellBounds.Y, stringFormat);
                            break;
                        }
                    }
                }
                else
                {
                    graphics.DrawString(toPrint[i].str, font, toPrint[i].brush, cellBounds.X + xOffset, cellBounds.Y, stringFormat);

                }
            }
            
        }

        /// <summary>
        /// represent string to print and colour to print it
        /// </summary>
        public struct StringPrintInfo
        {
            public String str;
            public Brush brush;
        }
    }



//DataSet
DataSet dataSet = new DataSet();
DataTable dStable = new DataTable();
DataColumn dSCol1 = new DataColumn("name", typeof(string));
DataColumn dSCol2 = new DataColumn("value", typeof(string));
DataRow rTemp;

//DataGridView
DataGridViewColumnCollection dGVColumnCollection;
DataGridViewColumn dGVColumn1 = new DataGridViewColumn();
DataGridViewColumn dGVColumn2 = new DataGridViewColumn();

public partial class Form1 : Form
{
    //DataSet
    DataSet dataSet = new DataSet();
    DataTable dStable = new DataTable();
    DataColumn dSCol1 = new DataColumn("name", typeof(string));
    DataColumn dSCol2 = new DataColumn("value", typeof(string));
    DataRow rTemp;

    //DataGridView
    DataGridViewColumnCollection dGVColumnCollection;
    DataGridViewColumn dGVColumn1 = new DataGridViewColumn();
    DataGridViewColumn dGVColumn2 = new DataGridViewColumn();



    public Form1()
    {
        InitializeComponent();

        //initialise dataset
        dStable.TableName = "sample";
        dStable.Columns.Add(dSCol1);
        dStable.Columns.Add(dSCol2);
        rTemp = dStable.NewRow();
        rTemp.ItemArray = new object[]{"bill", " 000 0000 0000000 0000000 0000000000"};
        dStable.Rows.Add(rTemp);
        rTemp = dStable.NewRow();
        rTemp.ItemArray = new object[]{"ted", " 111 1111 1111111 1111111 1111111111"};
        dStable.Rows.Add(rTemp);
        rTemp = dStable.NewRow();
        rTemp.ItemArray = new object[] { "john", " 222 2222 2222222 2222222 2222222222" };
        dStable.Rows.Add(rTemp);
        dataSet.Tables.Add(dStable);

        //add coloumns to datagridView
        dGVColumnCollection = dataGridView1.Columns;
        dGVColumnCollection.Add("col1", "Name");
        dGVColumnCollection.Add("col2", "Value");
        dGVColumn1 = dGVColumnCollection[0];
        dGVColumn2 = dGVColumnCollection[1];


        //pupulate DataGridView
        foreach(DataRow row in dataSet.Tables[0].Rows)
        {
            DataGridViewRow dGVRow = new DataGridViewRow();
            DataGridViewTextBoxCell textCell = new DataGridViewTextBoxCell();
            MultiColourDataGridViewTextBoxCell textCel2 = new MultiColourDataGridViewTextBoxCell();
            textCell.Value = row.ItemArray[0];
            textCel2.Value = row.ItemArray[1];
            dGVRow.Cells.Add(textCell);
            dGVRow.Cells.Add(textCel2);
            dataGridView1.Rows.Add(dGVRow);
        }

    }
}



http://img2.pict.com/b4/07/ad/2401377/0/gridcellcolour1.jpg
http://img2.pict.com/91/13/8d/2401376/0/gridcellcolour2.jpg

modified on Tuesday, January 5, 2010 9:17 PM

GeneralRe: Multi-color text in single cell of datagridview in C# Pin
auaryamane0075-Jan-10 13:32
auaryamane0075-Jan-10 13:32 
Questiontreeview allowdrop property not there Pin
CrimeanTurtle20084-Jan-10 12:31
CrimeanTurtle20084-Jan-10 12:31 
AnswerRe: treeview allowdrop property not there Pin
Mycroft Holmes4-Jan-10 13:34
professionalMycroft Holmes4-Jan-10 13:34 
GeneralRe: treeview allowdrop property not there Pin
CrimeanTurtle20084-Jan-10 13:42
CrimeanTurtle20084-Jan-10 13:42 
GeneralRe: treeview allowdrop property not there Pin
Mycroft Holmes4-Jan-10 14:01
professionalMycroft Holmes4-Jan-10 14:01 
GeneralRe: treeview allowdrop property not there Pin
CrimeanTurtle20084-Jan-10 14:20
CrimeanTurtle20084-Jan-10 14:20 
QuestionMovie from bmp files. bmp to tiff, mp4, or other formats Pin
ThorTheBraveGod4-Jan-10 8:43
ThorTheBraveGod4-Jan-10 8:43 
AnswerRe: Movie from bmp files. bmp to tiff, mp4, or other formats Pin
Migounette4-Jan-10 22:29
Migounette4-Jan-10 22:29 
GeneralRe: Movie from bmp files. bmp to tiff, mp4, or other formats Pin
ThorTheBraveGod5-Jan-10 6:32
ThorTheBraveGod5-Jan-10 6:32 
QuestionSliding Forms in windows mobile Pin
mahmoud ankeer4-Jan-10 7:14
mahmoud ankeer4-Jan-10 7:14 
QuestionRemoving values in a dictionary [modified] Pin
Paul Harsent4-Jan-10 4:55
Paul Harsent4-Jan-10 4:55 
AnswerRe: Removing values in a dictionary Pin
Luc Pattyn4-Jan-10 5:12
sitebuilderLuc Pattyn4-Jan-10 5:12 
AnswerRe: Removing values in a dictionary Pin
OriginalGriff4-Jan-10 5:27
mveOriginalGriff4-Jan-10 5:27 
GeneralRe: Removing values in a dictionary Pin
Paul Harsent4-Jan-10 5:32
Paul Harsent4-Jan-10 5:32 
GeneralRe: Removing values in a dictionary Pin
Luc Pattyn4-Jan-10 5:32
sitebuilderLuc Pattyn4-Jan-10 5:32 
AnswerRe: Removing values in a dictionary [modified] Pin
Luc Pattyn4-Jan-10 5:41
sitebuilderLuc Pattyn4-Jan-10 5:41 
AnswerRe: Removing values in a dictionary Pin
#realJSOP4-Jan-10 5:41
professional#realJSOP4-Jan-10 5:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.