Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. making two different style of datagridview's one cell

making two different style of datagridview's one cell

Scheduled Pinned Locked Moved C#
tutorialwpfcomquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Erdinc27
    wrote on last edited by
    #1

    hey guys..i want to change wordS' styles in a single cell of my datagridview for example in a cell i have "AsdAsd.CvbCvb"..i want it to make as AsdAsd CvbCvb..to do that i declared two cellstyle

    DataGridViewCellStyle mystyle1 = new DataGridViewCellStyle();
    DataGridViewCellStyle mystyle2 = new DataGridViewCellStyle();

    mystyle1.Font = new Font(dataGridView1.Font, FontStyle.Regular);
    mystyle2.Font = new Font(dataGridView1.Font, FontStyle.Italic);

    and to get the words in my cells i made this

    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
    for (int j = 1; j < dataGridView1.Columns.Count; j++)
    {
    string split = dataGridView1.Rows[i].Cells[j].Value.ToString();
    string[] split2 = split.Split(new char[] { '.' });

                    foreach (string item in split2)
                    {
                        dataGridView1.Rows\[i\].Cells\[j\].Value = split2\[0\].ToString()+"/\*/\*" + split2\[1\].ToString();
                    }
                }
            }
    

    but the point is that i dont know how to apply that styles to the words i got by splitting? can anyone suggest me a way

    vemedya.com

    _ H D 3 Replies Last reply
    0
    • E Erdinc27

      hey guys..i want to change wordS' styles in a single cell of my datagridview for example in a cell i have "AsdAsd.CvbCvb"..i want it to make as AsdAsd CvbCvb..to do that i declared two cellstyle

      DataGridViewCellStyle mystyle1 = new DataGridViewCellStyle();
      DataGridViewCellStyle mystyle2 = new DataGridViewCellStyle();

      mystyle1.Font = new Font(dataGridView1.Font, FontStyle.Regular);
      mystyle2.Font = new Font(dataGridView1.Font, FontStyle.Italic);

      and to get the words in my cells i made this

      for (int i = 0; i < dataGridView1.Rows.Count; i++)
      {
      for (int j = 1; j < dataGridView1.Columns.Count; j++)
      {
      string split = dataGridView1.Rows[i].Cells[j].Value.ToString();
      string[] split2 = split.Split(new char[] { '.' });

                      foreach (string item in split2)
                      {
                          dataGridView1.Rows\[i\].Cells\[j\].Value = split2\[0\].ToString()+"/\*/\*" + split2\[1\].ToString();
                      }
                  }
              }
      

      but the point is that i dont know how to apply that styles to the words i got by splitting? can anyone suggest me a way

      vemedya.com

      _ Offline
      _ Offline
      _Erik_
      wrote on last edited by
      #2

      I think you can create two classes, one inheriting from DataGridViewColumn and the other one from DataGridViewCell, where you can use a RichTextBox control to show your data with the format you need. I have never done this, but I guess it is not too hard. Read the documentation about these two classes. On the other hand, I would like to help you improve your code: All of these lines (I have added some comments):

      string split = dataGridView1.Rows[i].Cells[j].Value.ToString();
      string[] split2 = split.Split(new char[] { '.' }); // split.Split('.') is better

      // This loop is useless. Yoy can remove it
      foreach (string item in split2)
      {
      // Are you sure split2 array has two or more items?
      // and since split2 is a string[], there is no need of ToString() for split2[0] and split2[1]
      dataGridView1.Rows[i].Cells[j].Value = split2[0].ToString()+"/*/*" + split2[1].ToString();
      }

      Give the same result as:

      dataGridView1[j,i].Value = dataGridView1[j,i].Value.ToString().Replace(".", "/*/*");

      But there is a difference: if you do not have a "." char, your code would also throw an exception.

      1 Reply Last reply
      0
      • E Erdinc27

        hey guys..i want to change wordS' styles in a single cell of my datagridview for example in a cell i have "AsdAsd.CvbCvb"..i want it to make as AsdAsd CvbCvb..to do that i declared two cellstyle

        DataGridViewCellStyle mystyle1 = new DataGridViewCellStyle();
        DataGridViewCellStyle mystyle2 = new DataGridViewCellStyle();

        mystyle1.Font = new Font(dataGridView1.Font, FontStyle.Regular);
        mystyle2.Font = new Font(dataGridView1.Font, FontStyle.Italic);

        and to get the words in my cells i made this

        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
        for (int j = 1; j < dataGridView1.Columns.Count; j++)
        {
        string split = dataGridView1.Rows[i].Cells[j].Value.ToString();
        string[] split2 = split.Split(new char[] { '.' });

                        foreach (string item in split2)
                        {
                            dataGridView1.Rows\[i\].Cells\[j\].Value = split2\[0\].ToString()+"/\*/\*" + split2\[1\].ToString();
                        }
                    }
                }
        

        but the point is that i dont know how to apply that styles to the words i got by splitting? can anyone suggest me a way

        vemedya.com

        H Offline
        H Offline
        Henry Minute
        wrote on last edited by
        #3

        As far as I know there is no way to do this with the 'standard' DataGridViewTextBoxColumn or DataGridViewTextBoxCell. You can do it with the DataGridViewRichTextBoxColumn or Cell. RichTextBox Cell in a DataGridView[^], might help.

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        1 Reply Last reply
        0
        • E Erdinc27

          hey guys..i want to change wordS' styles in a single cell of my datagridview for example in a cell i have "AsdAsd.CvbCvb"..i want it to make as AsdAsd CvbCvb..to do that i declared two cellstyle

          DataGridViewCellStyle mystyle1 = new DataGridViewCellStyle();
          DataGridViewCellStyle mystyle2 = new DataGridViewCellStyle();

          mystyle1.Font = new Font(dataGridView1.Font, FontStyle.Regular);
          mystyle2.Font = new Font(dataGridView1.Font, FontStyle.Italic);

          and to get the words in my cells i made this

          for (int i = 0; i < dataGridView1.Rows.Count; i++)
          {
          for (int j = 1; j < dataGridView1.Columns.Count; j++)
          {
          string split = dataGridView1.Rows[i].Cells[j].Value.ToString();
          string[] split2 = split.Split(new char[] { '.' });

                          foreach (string item in split2)
                          {
                              dataGridView1.Rows\[i\].Cells\[j\].Value = split2\[0\].ToString()+"/\*/\*" + split2\[1\].ToString();
                          }
                      }
                  }
          

          but the point is that i dont know how to apply that styles to the words i got by splitting? can anyone suggest me a way

          vemedya.com

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          You can't use the styles because they apply to the ENTIRE cell, not just one little piece of content in it. You have to write your own custom DataGridViewColumn implementation to pull this off. There are examples out there, you just have to Google for something like "Custom DataGridViewColumn" for them.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          E 1 Reply Last reply
          0
          • D Dave Kreskowiak

            You can't use the styles because they apply to the ENTIRE cell, not just one little piece of content in it. You have to write your own custom DataGridViewColumn implementation to pull this off. There are examples out there, you just have to Google for something like "Custom DataGridViewColumn" for them.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            E Offline
            E Offline
            Erdinc27
            wrote on last edited by
            #5

            thanks for your asnwers guys..i will look at the examples and search more deeper.. because iam not that much professional yet to create a Custom DataGridView

            vemedya.com

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups