从SQL向GridView添加列 [英] Add column to GridView from SQL
本文介绍了从SQL向GridView添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想添加一列,其中包含从SQL中的表中检索到的行.
当用户从复选框列表中选择一个复选框(该列表具有SQL中该表的所有列)时,将知道要添加的列.
我正在执行以下操作,但是工作不正常:
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();
}
推荐答案
尝试使用数据集?
try using a DataSet?
public void LoadData()
{
DataSet DS = new DataSet();
dtapt.Fill(DS);
Mydt= DS.Tables[0];
}
这是我使用的用于SQLite的概念,但基本相同.
This is what I Use It''s For SQLite but basically the same concept.
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;");
}
您可以在选中chechkbox之后执行此操作
u can do like this after chechkbox checked
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();
}
}
这篇关于从SQL向GridView添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文