Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to display sql server table structure in datagrid grid.
I want to display table structure in datagrid .

Like
Column Name  Data type       Allow Null
id           (Numeric 18,0) 
roll_no      (Numeric 18,0) 
class         Varchar(50)
Name          Varchar(50)
Fname         Varchar(50)


[edit]Code block added, spurious bold removed - OriginalGriff[/edit]
Posted
Updated 21-Oct-12 21:09pm
v2

Try:
SQL
SELECT column_name, data_type, is_nullable, character_maximum_length FROM information_schema.COLUMNS WHERE table_name='myTable'
That returns the table definition as normal SQL data - so just display it as you would any other data.
 
Share this answer
 
Hello try this one,

SQL
Select  t.Table_Schema,
        t.Table_Name,
        c.Column_Name,
        IsNull(c.Column_Default, '') as 'Column_Default',
        c.Is_Nullable,
        c.Data_Type,
        IsNull(c.Character_Maximum_Length, IsNull(Numeric_Precision,'') + IsNull(Numeric_Scale, IsNull(DateTime_Precision,''))) as 'Size'

From Information_Schema.Tables t

Join Information_Schema.Columns c on    t.Table_Catalog = c.Table_Catalog
                                And     t.Table_Schema = c.Table_Schema
                                And     t.Table_Name = c.Table_Name

Where t.Table_Type = 'BASE TABLE' and t.Table_Name='your table name'

Order by t.Table_Schema, t.Table_Name, c.Ordinal_Position


c# Code:

C#
void FillData()
    {
        // 1
        // Open connection
        using (SqlCeConnection c = new SqlCeConnection(
        your connection string))
        {
        c.Open();
        // 2
        // Create new DataAdapter
        using (SqlCeDataAdapter a = new SqlCeDataAdapter(
            "your Query", c))
        {
            // 3
            // Use DataAdapter to fill DataTable
            DataTable t = new DataTable();
            a.Fill(t);
            // 4
            // Render data onto the screen
            dataGridView1.DataSource = t;
        }
        }
    }

Call this in your page load or button click an so on...

regards
sarva
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900