我有一個數據表,然後我將數據表的所有列名作爲複選框,我只想顯示那些用戶從複選框中選擇的列記錄:
下面是的代碼,但我沒有得到期望的結果複製一個數據表選擇列到另一個數據表
var values = "";
string clmnm = "";
for (int i = 0; i < interestedIN.Items.Count; i++)
{
if (interestedIN.Items[i].Selected)
{
values += interestedIN.Items[i].Value + ",";
}
}
values = values.TrimEnd(',');
string[] words = values.Split(',');
DataTable dt = new DataTable();
dt = (DataTable)Session["dataset"];
DataTable dt1 = new DataTable();
foreach (string word in words)
{
dt1.Columns.Add(word, typeof(string));
if (clmnm == string.Empty)
{
clmnm = word.Trim();
}
else
{
clmnm += "," +word.Trim();
}
}
foreach (DataRow dr in dt.Rows)
{
string[] split = clmnm.Split(',');
int j =0;
string str = "";
string str2 = "";
while(j < split.Length)
{
str = split[j].ToString();
if (str2 == string.Empty)
{
str2 = "dr[\""+str.ToString()+"\"]";
}
else
{
str2 += "," + "dr[\""+str.ToString()+"\"]";
}
j+=1;
}
dt1.Rows.Add(str2);
}
然後我想結果導出爲Excel工作表:但得到下面的Excel表:
您是如何顯示結果的? 'GridView'? – Bharadwaj
不,實際上我是將結果導出爲excel表格..我的意思是dt1表格.. – Srikanta
我添加了一些更新.. – Srikanta