Hi,

I have a sql statemente but I don't want to display all info in the gridview (c# webforms). How to bind specific columns at runtime on gridview?

Thanks

Recommended Answers

All 2 Replies

well, why dont you just select the columns that you want to see, then bind them???

Are you using OLEDB or SQLCommand?

OLEDB:

   OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileAddress.Text + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;MAXSCANROWS=0\"");
   OleDbDataAdapter myCommand = new OleDbDataAdapter(" SELECT [Terminal Numbers] FROM [To be Scheduled$]", conn);

      try
      {
        AddTermTable.Clear();
        myCommand.Fill(AddTermTable);
        AddTermGrid.DataSource = AddTermTable;
        QuanCount2.Clear();
        string NumOfDevices = AddTermTable.Rows.Count.ToString();
        ileName = NumOfDevices;
       }

SQLCommand/Connection:

DataSet dsData = new DataSet();
       SqlConnection thisConnection = null;
       thisConnection = new SqlConnection(@"Network Library = DBMSSOCN; Data Source=prl-sql08-01; Initial Catalog=RemoteDownload; integrated security = true");
       SqlCommand thisCommand = thisConnection.CreateCommand();
       dsData.Clear();
       SqlDataAdapter adapter = new SqlDataAdapter("select Date_Scheduled, count(*) as 'Quantity' from tbl_" + Release.Text + "_DOWNLOAD group by Date_Scheduled order by Date_Scheduled", thisConnection);//" + Release.Text + "
       thisConnection.Open();
       adapter.Fill(dsData);
       AddTermGrid.DataSource = dsData.Tables[0];
       thisConnection.Close();

I have a sql statemente but I don't want to display all info in the gridview (c# webforms). How to bind specific columns at runtime on gridview?

create a datatable and populate it with the columns you need

Ex:

I have a sql statemente but I don't want to display all info in the gridview (c# webforms). How to bind specific columns at runtime on gridview?

Da

DataTable dtSource = new DataTable;
dtSource.Load(yourDataReader);

DataTable dt = new DataTable();
dt.Collumns.Add("Name", typeof(string));

for (int i=0; i<dtSource.Rows.Count; i++)
{
    dt.Rows.Add(dtSource.Rows[i][1].ToString());
}

and use the dt as datasource for your gridview

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.