DataGridView: Displaying empty cells for certain values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ashutosh

    DataGridView: Displaying empty cells for certain values

    Hi,
    I have a DataGridView which has as typed data-set associated with it.
    For the table which I am displaying in it, I want to display certain
    cells (corresponding to a column in the database) to be displayed as
    blank instead of the value 0 (Zero - integer). Also, I want the value 0
    (zero) to be saved to the database when the changes are applied/saved
    and if the cells are blank.

    I tried to change the text that is drawn/shown in the grid by handling
    the CellPainting event, but the Value and FormattedValue properties are
    read only.

    How can I achieve this?

    Thanks & Regards,
    Ashutosh
  • Matthias Krug

    #2
    Re: DataGridView: Displaying empty cells for certain values

    Ashutosh schrieb:
    Hi,
    I have a DataGridView which has as typed data-set associated with it.
    For the table which I am displaying in it, I want to display certain
    cells (corresponding to a column in the database) to be displayed as
    blank instead of the value 0 (Zero - integer). Also, I want the value 0
    (zero) to be saved to the database when the changes are applied/saved
    and if the cells are blank.
    >
    I tried to change the text that is drawn/shown in the grid by handling
    the CellPainting event, but the Value and FormattedValue properties are
    read only.
    >
    How can I achieve this?
    How about a quick and dirty one?
    Use the CellPainting to make ForeColor=BackC olor for those Cells with a
    zero in them :)

    Comment

    • Zhi-Xin Ye [MSFT]

      #3
      RE: DataGridView: Displaying empty cells for certain values

      Hi Ashutosh,

      You can handle the CellFormatting event instead. For example:

      void dataGridView1_C ellFormatting(o bject sender,
      DataGridViewCel lFormattingEven tArgs e)
      {
      if (e.ColumnIndex == 0 && e.Value != null)
      {
      if ((int)e.Value == 0)
      {
      e.Value = "";
      }
      }
      }


      For more information about the CellFormatting event , you can refer to this
      document:

      CellFormatting event
      Gain technical skills through documentation and training, earn certifications and connect with the community

      llformatting.as px

      Please try my suggestion, and let me know the result.

      Sincerely,
      Zhi-Xin Ye
      Microsoft Managed Newsgroup Support Team

      Delighting our customers is our #1 priority. We welcome your comments and
      suggestions about how we can

      improve the support we provide to you. Please feel free to let my manager
      know what you think of the level

      of service provided. You can send feedback directly to my manager at:
      msdnmg@microsof t.com.

      =============== =============== =============== =====
      Get notification to my posts through email? Please refer to

      http://msdn.microsoft.com/en-us/subs...#notifications.

      Note: MSDN Managed Newsgroup support offering is for non-urgent issues
      where an initial response from the

      community or a Microsoft Support Engineer within 2 business day is
      acceptable. Please note that each follow

      up response may take approximately 2 business days as the support
      professional working with you may need

      further investigation to reach the most efficient resolution. The offering
      is not appropriate for situations

      that require urgent, real-time or phone-based interactions. Issues of this
      nature are best handled working

      with a dedicated Microsoft Support Engineer by contacting Microsoft
      Customer Support Services (CSS) at


      =============== =============== =============== =====
      This posting is provided "AS IS" with no warranties, and confers no rights.


      Comment

      • Ashutosh

        #4
        Re: DataGridView: Displaying empty cells for certain values

        Hi Matthias,

        Actually that was the first solution that came to my mind. But the
        problem with that approach is that the value of zero will still be there
        and user can select/copy it. Thanks anyways :)

        Thanks & Regards,
        Ashutosh

        Matthias Krug wrote:
        Ashutosh schrieb:
        >Hi,
        >I have a DataGridView which has as typed data-set associated with it.
        >For the table which I am displaying in it, I want to display certain
        >cells (corresponding to a column in the database) to be displayed as
        >blank instead of the value 0 (Zero - integer). Also, I want the value
        >0 (zero) to be saved to the database when the changes are
        >applied/saved and if the cells are blank.
        >>
        >I tried to change the text that is drawn/shown in the grid by
        >handling the CellPainting event, but the Value and FormattedValue
        >properties are read only.
        >>
        >How can I achieve this?
        >
        How about a quick and dirty one?
        Use the CellPainting to make ForeColor=BackC olor for those Cells with
        a zero in them :)

        Comment

        • Ashutosh

          #5
          Re: DataGridView: Displaying empty cells for certain values

          Hi Zhi,
          Thanks for the information. I need little more help here.

          For these columns, if the user doesn't enter any value (while editing or
          adding a record), a value of NULL is added to the database. How can
          override this so that I update the database with value of Zero and still
          display an empty cell in the grid.

          Thanks & Regards,
          Ashutosh

          Zhi-Xin Ye [MSFT] wrote:
          Hi Ashutosh,
          >
          You can handle the CellFormatting event instead. For example:
          >
          void dataGridView1_C ellFormatting(o bject sender,
          DataGridViewCel lFormattingEven tArgs e)
          {
          if (e.ColumnIndex == 0 && e.Value != null)
          {
          if ((int)e.Value == 0)
          {
          e.Value = "";
          }
          }
          }
          >
          >
          For more information about the CellFormatting event , you can refer to this
          document:
          >
          CellFormatting event
          Gain technical skills through documentation and training, earn certifications and connect with the community

          llformatting.as px
          >
          Please try my suggestion, and let me know the result.
          >
          Sincerely,
          Zhi-Xin Ye
          Microsoft Managed Newsgroup Support Team
          >
          Delighting our customers is our #1 priority. We welcome your comments and
          suggestions about how we can
          >
          improve the support we provide to you. Please feel free to let my manager
          know what you think of the level
          >
          of service provided. You can send feedback directly to my manager at:
          msdnmg@microsof t.com.
          >
          =============== =============== =============== =====
          Get notification to my posts through email? Please refer to
          >
          http://msdn.microsoft.com/en-us/subs...#notifications.
          >
          Note: MSDN Managed Newsgroup support offering is for non-urgent issues
          where an initial response from the
          >
          community or a Microsoft Support Engineer within 2 business day is
          acceptable. Please note that each follow
          >
          up response may take approximately 2 business days as the support
          professional working with you may need
          >
          further investigation to reach the most efficient resolution. The offering
          is not appropriate for situations
          >
          that require urgent, real-time or phone-based interactions. Issues of this
          nature are best handled working
          >
          with a dedicated Microsoft Support Engineer by contacting Microsoft
          Customer Support Services (CSS) at
          >

          =============== =============== =============== =====
          This posting is provided "AS IS" with no warranties, and confers no rights.
          >
          >
          >

          Comment

          • Zhi-Xin Ye [MSFT]

            #6
            Re: DataGridView: Displaying empty cells for certain values

            Hi Ashutosh,

            You can handle the CellParsing event.

            DataGridView.Ce llParsing event
            Gain technical skills through documentation and training, earn certifications and connect with the community

            llparsing.aspx

            If you have any questions or concerns, please don't hesitate to let me know.

            Sincerely,
            Zhi-Xin Ye
            Microsoft Managed Newsgroup Support Team

            Delighting our customers is our #1 priority. We welcome your comments and
            suggestions about how we can

            improve the support we provide to you. Please feel free to let my manager
            know what you think of the level

            of service provided. You can send feedback directly to my manager at:
            msdnmg@microsof t.com.

            This posting is provided "AS IS" with no warranties, and confers no rights.

            Comment

            Working...