Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Dynamic Table in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 288
    Comment on it

    While retriving or inserting data into the database we use SqlClasses.

    But we can also create dynamic data or dynamic table if we want.

     

    For doing that we need to use the classes:

     

    DataTable : Table where rows and column are stored.

     

    DataTable dtTable=new DataTable();

     

    DataColumn : For creating column.

     

    DataColumn dtColumn=new DataColumn();

     

    DataRow: For creating row.

     

    DataRow dtRow=new DataRow();

     

    We can create rows and then column as many as we want and will define column datatype.

     

    For adding column into the row we use Add method for it.

     

     

    / Create new DataTable and DataSource objects.
        DataTable table = new DataTable();
    
        // Declare DataColumn and DataRow variables.
        DataColumn column;
        DataRow row; 
        DataView view;
    
        // Create new DataColumn, set DataType, ColumnName and add to DataTable.    
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.Int32");
        column.ColumnName = "id";
        table.Columns.Add(column);
    
        // Create second column.
        column = new DataColumn();
        column.DataType = Type.GetType("System.String");
        column.ColumnName = "item";
        table.Columns.Add(column);
    
        // Create new DataRow objects and add to DataTable.    
        for(int i = 0; i < 10; i++)
        {
            row = table.NewRow();
            row["id"] = i;
            row["item"] = "item " + i.ToString();
            table.Rows.Add(row);
        }
    
        // Create a DataView using the DataTable.
        view = new DataView(table);
    
        // Set a DataGrid control's DataSource to the DataView.
        dataGrid1.DataSource = view;
    

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: