Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add a column with the row retrieved from the a table in SQL.
The column to be added will be known when the user select a checkbox from a checkbox list which has all the columns from that table in SQL.

What I am doing is the following, but is not working properly:
private void LoadData()
{
    OpenConn();
    string MyTable = "SELECT * FROM [MyTable]";
    SqlDataAdapter dtadpt = new SqlDataAdapter(MyTable, Connection.destConn);
    dtadpt.Fill(Mydt);
    dtadpt.Dispose();
    CloseConn();
}

private void ShowDailyTable()
{
         BoundField colName = new BoundField();
         int index = 0;

         if (CheckBoxList1.SelectedValue.ToString() == "Bar")
         {
             colName.DataField = "Bar";
             index = Mydt.Columns["Bar"].Ordinal;
             colName.HeaderText = Mydt.Rows[0][index].ToString();
             Menu1.Columns.Add(colName);
         }
         Menu1.DataSource = Mydt;
         Menu1.DataBind();
}
Posted
Updated 18-Apr-11 12:56pm
v2

try using a DataSet?
C#
public void LoadData()
    {
        DataSet DS = new DataSet();
        dtapt.Fill(DS); 
        Mydt= DS.Tables[0];
    }
 
Share this answer
 
Comments
jalmonte 18-Apr-11 18:55pm    
The column and the row is not displaying on the GridView
charles henington 18-Apr-11 22:06pm    
Did You try to add to a DataSet then insert the DataSet into DataTable?
This is what I Use It's For SQLite but basically the same concept.

C#
public class Form1 : Form
       {
		private DataGrid Grid;	
		private SQLiteConnection sql_con;
		private SQLiteCommand sql_cmd;
		private SQLiteDataAdapter DB;
		private DataSet DS = new DataSet();
		private DataTable DT = new DataTable();
        }

        private void Form1_Load(object sender, EventArgs e)
	{
		LoadData();
	}
        private void LoadData()
	{
		SetConnection(); 
		sql_con.Open();
		sql_cmd = sql_con.CreateCommand();
		string CommandText = "select Url, Url from  rss";
		DB = new SQLiteDataAdapter(CommandText,sql_con);
                DS.Reset();
		DB.Fill(DS);            
		DT= DS.Tables[0];
		Grid.DataSource = DT;
		sql_con.Close();            
	}
        private void SetConnection()
        {
		sql_con = new SQLiteConnection("Data                           
		Source=Rss.db;Version=3;New=False;Compress=True;");
        }
 
Share this answer
 
u can do like this after chechkbox checked

SQL
if (CheckBoxList1.SelectedValue.ToString() == "Bar")
         {

       Mydt.Columns.Add("Your Column Name");
       for(int i=0;i<Mydt.Rows.Count;i++)
       {
           Mydt.Rows[i]["Your Column Name"] = (i+1).ToString();
       }
}
 
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