我无法将DataSet转换为名为PrefixDescription的webform文本框。我试图将行转换为字符串,然后我尝试将字符串放入文本框中。但是,文本框中没有任何内容。 DataSet确实有数据。我尝试过数据绑定和数据绑定,但这些都不起作用。
private DirectoryEntry testAD = new DirectoryEntry();
private DataTable DT = new DataTable();
protected void Button2_Click(object sender, EventArgs e)
{
DirectorySearcher search = new DirectorySearcher(testAD);
SearchResultCollection myResults = search.FindAll();
search.PropertiesToLoad.Add("name");
DT.Columns.Add("name");
DT.Columns.Add();
foreach (SearchResult SR in myResults)
{
DataRow dr = DT.NewRow();
DirectoryEntry DE = SR.GetDirectoryEntry();
dr["name"] = DE.Properties["name"].Value;
DT.Rows.Add(dr);
DT.AcceptChanges();
PrefixDescription.Text = Convert.ToString(dr["name"]);
DE.Close();
}
}
答案 0 :(得分:1)
更好的是,使用StringBuilder
,类似这样......
System.Text.StringBuilder builder = new System.Text.StringBuilder();
foreach (SearchResult SR in myResults)
{
DataRow dr = DT.NewRow();
DirectoryEntry DE = SR.GetDirectoryEntry();
dr["name"] = DE.Properties["name"].Value;
DT.Rows.Add(dr);
DT.AcceptChanges();
builder.Append(Convert.ToString(dr["name"]));
PrefixDescription.Text = Convert.ToString(dr["name"]);
DE.Close();
}
PrefixDescription.Text = builder.ToString();