我一直在尝试在第1列的每个单元格之间进行换行:
值1,值2,
价值3
然而,换行符只是在单元格中显示为纯文本:
<asp:GridView ID="dg1" runat="server" AllowSorting="true" OnSorting="Sort" AutoGenerateColumns="false" OnSelectedIndexChanged="dg1_SelectedIndexChanged" CssClass="DGgeneral" HeaderStyle-CssClass="DGheader" RowStyle-CssClass="DGrow1" AlternatingRowStyle-CssClass="DGrow2" >
<Columns>
</Columns>
</asp:GridView>
我正在以编程方式执行所有操作,因此我无法将HTMLEncode="false"
添加到BoundFields,这是一个巨大的痛苦。
public void SearchQuery(string sOrderBy, string sOrderByColumn)
{
//Create and Clear Datatable and Gridview
dt.Clear();
dg1.DataSource = null;
dg1.DataBind();
//Initialise Strings
string sTLTaskID = "";
string sTitle = "";
string sForename = "";
string sSurname = "";
string sEmail = "";
string sMobile = "";
string sHome = "";
string sDateTime = "";
string sState = "";
string sFormName = "";
//Initialise Column Names
dt.Columns.Add("Id/Form/State");
dt.Columns.Add("Title");
dt.Columns.Add("Forename");
dt.Columns.Add("Surname");
dt.Columns.Add("Email address");
dt.Columns.Add("Telephone 1");
dt.Columns.Add("Telephone 2");
dt.Columns.Add("Date & Time");
//Connect to ********
using (var connection = new SqlConnection(
"Data Source = ******;" +
"Integrated Security = ******;" +
"persist security info = ******;" +
"Initial Catalog = ********;"))
{
//Establish SQL Command
using (var command = connection.CreateCommand())
{
command.CommandText = "********";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@TLTaskID", txtTLTaskID.Text);
command.Parameters.AddWithValue("@Name", txtName.Text);
command.Parameters.AddWithValue("@Email", txtEmail.Text);
command.Parameters.AddWithValue("@MNumber", txtNumber.Text);
command.Parameters.AddWithValue("@HNumber", txtNumber.Text);
if ((sOrderBy == null) || (sOrderBy == ""))
{
command.Parameters.AddWithValue("@AscDesc", "");
}
else if (sOrderBy == "Asc")
{
command.Parameters.AddWithValue("@AscDesc", "Asc");
}
else if (sOrderBy == "Desc")
{
command.Parameters.AddWithValue("@AscDesc", "Desc");
}
if ((sOrderByColumn == null) || (sOrderByColumn == ""))
{
command.Parameters.AddWithValue("@OrderByColumn", "");
}
else
{
command.Parameters.AddWithValue("@OrderByColumn", sOrderByColumn);
}
connection.Open();
//Initialise Column Names
using (var reader = command.ExecuteReader())
{
var column1 = reader.GetOrdinal("TLTaskID");
var column2 = reader.GetOrdinal("Title");
var column3 = reader.GetOrdinal("FirstName");
var column4 = reader.GetOrdinal("Surname");
var column5 = reader.GetOrdinal("Email");
var column6 = reader.GetOrdinal("Telephone1");
var column7 = reader.GetOrdinal("Telephone2");
var column8 = reader.GetOrdinal("DateTime");
var column9 = reader.GetOrdinal("State");
var column10 = reader.GetOrdinal("FormName");
//Loop until out of Rows in table
while (reader.Read())
{
//Set values of each row to a variable
var TLTaskID = reader.GetValue(column1);
var Title = reader.GetValue(column2);
var Forename = reader.GetValue(column3);
var Surname = reader.GetValue(column4);
var Email = reader.GetValue(column5);
var Mobile = reader.GetValue(column6);
var Home = reader.GetValue(column7);
var DateTime = reader.GetValue(column8);
var State = reader.GetValue(column9);
var FormName = reader.GetValue(column10);
//Convert to strings //Possible speed up ****
sTLTaskID = TLTaskID.ToString();
sTitle = Title.ToString();
sForename = Forename.ToString();
sSurname = Surname.ToString();
sEmail = Email.ToString();
sMobile = Mobile.ToString();
sHome = Home.ToString();
sDateTime = DateTime.ToString();
sState = State.ToString();
sFormName = FormName.ToString();
//Add to DataTable
var dr = dt.NewRow();
dr["Id/Form/State"] = sTLTaskID + ", " + sFormName + ", " + sState;
dr["Title"] = sTitle;
dr["Forename"] = sForename;
dr["Surname"] = sSurname;
dr["Email address"] = sEmail;
dr["Telephone 1"] = sMobile;
dr["Telephone 2"] = sHome;
dr["Date & Time"] = sDateTime;
dt.Rows.Add(dr);
//Set GridView's datasource to Datatable dr
dg1.DataSource = dt;
dg1.DataBind();
}
}
}
}
int iRowCount = dt.Rows.Count;
if (iRowCount > 0)
{
lblRecordTotal.Text = "Records: 1 - " + iRowCount.ToString() + " of " + iRowCount.ToString();
}
else
{
lblRecordTotal.Text = "No records found.";
}
}
如果需要更多内容,请随时离开。
有什么想法吗?
答案 0 :(得分:2)
在GridView的RowDataBound事件中添加以下行:
e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace("\n", "<br/>");
注意:您必须将OnRowDataBound事件添加到GridView,如下所示:
<asp:GridView ID="dg1" runat="server" OnRowDataBound="dg1_RowDataBound">