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