c# - How to set maximum size of a column in a DataTable

Set the maximum size of a DataColumn in a DataTable
The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must first use the NewRow() method to return a new DataRow object. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data.

The following asp.net c# tutorial code demonstrates how we can set the maximum size of a DataColumn in a DataTable instance. Here we used the DataColumn class MaxLength property to set the maximum size/length of a DataColumn in a DataTable object.

The DataColumn class MaxLength property gets or sets the maximum length of a text column. The MaxLength property value is an Int32 which is the maximum length of the column in characters. If the specified column has no maximum length, the property value is -1 and this is the default value.

But the DataColumn MaxLength property is ignored for non-text columns. The ArgumentException exception is raised if the asp.net developers assign MaxLength to a non-string column.
DataColumnMaxLengthProperty.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html>
<script runat="server">
    void Button1_Click(object sender, System.EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.TableName = "Books";
        
        DataColumn dc = new DataColumn();
        dc.ColumnName = "BookID";
        dc.DataType = typeof(int);
        dt.Columns.Add(dc);

        DataColumn dc2 = new DataColumn();
        dc2.ColumnName = "BookName";
        dc2.DataType = typeof(string);
        //this line make DataColumn values maximum length 40 characters
        dc2.MaxLength = 40;
        dt.Columns.Add(dc2);

        DataColumn dc3 = new DataColumn();
        dc3.ColumnName = "BookAuthor";
        dc3.DataType = typeof(string);
        dt.Columns.Add(dc3);

        dt.Rows.Add(new object[] { 1, "Programming Python, Fourth Edition", "Mark Lutz" });
        dt.Rows.Add(new object[] { 2, "Head First Python", "Paul Barry" });

        /*uncomment this line to get error message because 
        BookName column maximum length 40 characters*/
        //dt.Rows.Add(new object[] { 3, "Hadoop: The Definitive Guide, Second Edition", "Tom White" });

        dt.Rows.Add(new object[] { 4, "Real World Instrumentation with Python", "John M. Hughes" });

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use DataColumn MaxLength Property in ado.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkBlue; font-style:italic;">
            How to use DataColumn MaxLength Property in ado.net
        </h2>
        <hr width="525" align="left" color="CornFlowerBlue" />
        <asp:GridView 
            ID="GridView1"
            runat="server"
            BorderColor="Snow"
            ForeColor="Snow"
            Width="550"
            >
            <HeaderStyle BackColor="Crimson" ForeColor="Snow" Height="30" />
            <RowStyle BackColor="SaddleBrown" ForeColor="Snow" />
            <AlternatingRowStyle BackColor="Tan" ForeColor="Snow" />
        </asp:GridView>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Populate GridView"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More c# examples