Question:
how i can display table structure on data grid view in c# windows application with sql server?
Nidhi
2011-04-07 01:50:56 UTC
how i can display table structure on data grid view in c# windows application with sql server?
Five answers:
Salim Sodha
2011-04-07 11:17:13 UTC
Hi................

Hope This works

---------------------------------------



using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

// Add Namespace for sql server

using System.Data.SqlClient;



namespace WindowsFormsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

SqlConnection con = new SqlConnection(@"Data Source=salim-pc\SALIM_SQL;Initial Catalog=W3school;Integrated Security=True");

private void Form1_Load(object sender, EventArgs e)

{

SqlDataAdapter da = new SqlDataAdapter("select cus_id,cus_company,cus_country from customers",con);

DataTable dt = new DataTable();

da.Fill(dt);



dataGridView1.DataSource = dt;





}

}

}





Regards...

Salim Sodha
duggs
2016-12-11 09:17:43 UTC
Gridview In Windows Application
Sally
2011-04-07 02:27:53 UTC
Here is an example:

using System;

using System.Data;



class Program

{

static void Main()

{

//

// Get the DataTable.

//

DataTable table = GetTable();

//

// Use DataTable here with SQL, etc.

//

}



///

/// This example method generates a DataTable.

///


static DataTable GetTable()

{

//

// Here we create a DataTable with four columns.

//

DataTable table = new DataTable();

table.Columns.Add("Dosage", typeof(int));

table.Columns.Add("Drug", typeof(string));

table.Columns.Add("Patient", typeof(string));

table.Columns.Add("Date", typeof(DateTime));



//

// Here we add five DataRows.

//

table.Rows.Add(25, "Indocin", "David", DateTime.Now);

table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);

table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);

table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);

table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);

return table;

}

}



Try to google instead of just asking unclear questions.
Linda
2016-04-30 05:12:36 UTC
First, use the SqlConnection object to connect to the database. Then, use the SqlCommand object to call the function.
Serge M
2011-04-07 08:27:47 UTC
This is direction:



select COLUMN_NAME, DATA_TYPE , IS_NULLABLE

from INFORMATION_SCHEMA.COLUMNS

where TABLE_NAME ='yourTable'


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...