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. add data in datarow and fit max length columns

add data in datarow and fit max length columns

Scheduled Pinned Locked Moved C#
helpquestion
2 Posts 1 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.
  • J Offline
    J Offline
    JoZ CaVaLLo
    wrote on last edited by
    #1

    Hi body... I have the following problem: I fill a datarow with some data coming from another program, when I try to do DataTable.AddMyRow(Myrow), I get the following exception: Cannot set column 'BeneficiaryName'. The value violates the MaxLength limit of this column. Is there a way to set the DataTable in order to "auto truncate" the exceeding length? Thank you

    Life is not short... the problem is only how you organize yourself

    J 1 Reply Last reply
    0
    • J JoZ CaVaLLo

      Hi body... I have the following problem: I fill a datarow with some data coming from another program, when I try to do DataTable.AddMyRow(Myrow), I get the following exception: Cannot set column 'BeneficiaryName'. The value violates the MaxLength limit of this column. Is there a way to set the DataTable in order to "auto truncate" the exceeding length? Thank you

      Life is not short... the problem is only how you organize yourself

      J Offline
      J Offline
      JoZ CaVaLLo
      wrote on last edited by
      #2

      I found my own answer... I have to use the ColumnChanging event on the table and use the following code:

      void ColumnChanging(object sender, DataColumnChangeEventArgs e)
      {
      if (e.Column.DataType == typeof(string))
      {
      if (e.ProposedValue.ToString().Length > e.Column.MaxLength)
      e.ProposedValue = e.ProposedValue.ToString().Substring(0, e.Column.MaxLength - 1);
      }
      }

      Life is not short... the problem is only how you organize yourself

      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