views:

48

answers:

1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Khajohnsubdee.Web.DatagridByQuery
{
    public class DatagridByQuery
    {

        public string strConnect
        {
            set;
            get;
        }

        public DataSet GetDataSource(string sqlQuery, string dataSourceName)
        {
            var dataSet = new DataSet(dataSourceName);
            SqlConnection sc = null;
            try
            {
                var sqlConnection = new SqlConnection(strConnect);
                sqlConnection.Open();
                var sa = new SqlDataAdapter(new SqlCommand(sqlQuery, sc));
                sa.Fill(dataSet);
            } catch
            {
            }
            finally
            {
                if (sc != null) sc.Close();

            }
            return dataSet;
        }
    }
}

this my code

can use dataGrid1.Fill(new DatagridByQuery("Select * From Test", "test1"));

? thanks

A: 

try using this...

dataGrid1.DataSource = ds.Tables["yourtablename"].DefaultView; //yourtablename can be set table index
    dataGrid1.DataBind();
Muhammad Akhtar