根据 SQL 数据 Reader 格式化 Gridview 上的单元格
Formatting Cells on Gridview from SQL Data Reader
我有一个网格视图,它填充了我的 SQL 数据 reader
网格视图:
<asp:GridView ID="gridviewALL" runat="server" OnItemDataBound="Search_ItemDataBound">
</asp:GridView>
SQL数据Reader:
SqlCommand cmd = new SqlCommand("SELECT en.dpCreatedDT AS 'Time Received', en.enStatusCH AS 'Status', en.enNotificationNoNI AS 'LSBUD Ref', cm.cmpersonfirstch AS 'First Name', cm.cmPersonLastCH AS 'Last Name', cm.cmcompanynamech AS 'Company' FROM dp_enquiry en JOIN dp_caller_master cm ON (en.encmcallerkeyfk = cm.cmCallerKeyNI) WHERE en.ennotificationnoni = @JobnoALL", conn);
try
{
SqlParameter search = new SqlParameter();
search.ParameterName = "@JobnoALL";
search.Value = JobnoALL.Text.Trim();
cmd.Parameters.Add(search);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
gridviewALL.DataSource = dt;
gridviewALL.DataBind();
}
我试图在文本等于某个值时更改网格视图中单元格的格式,我之前使用列表视图完成过此操作,但网格视图步骤似乎有所不同。我有以下似乎没有任何建议?
private void Search_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
string CurrentColumn = e.Item.Cells[1].Text;
if (CurrentColumn == "PROC")
{
e.Item.Cells[1].Text = "Creating PDF";
}
else if (CurrentColumn == "CLOS")
{
e.Item.Cells[1].Text = "Complete";
e.Item.Cells[1].ForeColor = System.Drawing.Color.Green;
}
}
它一定是在读表头,你需要检查它是否是 DataRow
:-
if (e.Row.RowType == DataControlRowType.DataRow)
{
string CurrentColumn = e.Item.Cells[1].Text;
//your code goes here..
}
此外,我建议您改用 DataBinder.Eval 方法来避免对单元格索引进行硬编码,因为如果列的顺序发生变化,它可能会导致错误。
string CurrentColumn = DataBinder.Eval(e.Row.DataItem, "yourColumnName").ToString();
更新:
刚注意到您正在使用 ItemDataBound
,这是 DataGrid
的事件,而不是 Gridview
。使用 RowDataBound
事件代替:-
<asp:GridView ID="gridviewALL" runat="server" OnRowDataBound="gridviewALL_RowDataBound">
</asp:GridView>
您的 rowDataBound 事件应如下所示:-
protected void gridviewALL_RowDataBound(object sender, GridViewRowEventArgs e)
{
//your code here
}
我有一个网格视图,它填充了我的 SQL 数据 reader
网格视图:
<asp:GridView ID="gridviewALL" runat="server" OnItemDataBound="Search_ItemDataBound">
</asp:GridView>
SQL数据Reader:
SqlCommand cmd = new SqlCommand("SELECT en.dpCreatedDT AS 'Time Received', en.enStatusCH AS 'Status', en.enNotificationNoNI AS 'LSBUD Ref', cm.cmpersonfirstch AS 'First Name', cm.cmPersonLastCH AS 'Last Name', cm.cmcompanynamech AS 'Company' FROM dp_enquiry en JOIN dp_caller_master cm ON (en.encmcallerkeyfk = cm.cmCallerKeyNI) WHERE en.ennotificationnoni = @JobnoALL", conn);
try
{
SqlParameter search = new SqlParameter();
search.ParameterName = "@JobnoALL";
search.Value = JobnoALL.Text.Trim();
cmd.Parameters.Add(search);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
gridviewALL.DataSource = dt;
gridviewALL.DataBind();
}
我试图在文本等于某个值时更改网格视图中单元格的格式,我之前使用列表视图完成过此操作,但网格视图步骤似乎有所不同。我有以下似乎没有任何建议?
private void Search_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
string CurrentColumn = e.Item.Cells[1].Text;
if (CurrentColumn == "PROC")
{
e.Item.Cells[1].Text = "Creating PDF";
}
else if (CurrentColumn == "CLOS")
{
e.Item.Cells[1].Text = "Complete";
e.Item.Cells[1].ForeColor = System.Drawing.Color.Green;
}
}
它一定是在读表头,你需要检查它是否是 DataRow
:-
if (e.Row.RowType == DataControlRowType.DataRow)
{
string CurrentColumn = e.Item.Cells[1].Text;
//your code goes here..
}
此外,我建议您改用 DataBinder.Eval 方法来避免对单元格索引进行硬编码,因为如果列的顺序发生变化,它可能会导致错误。
string CurrentColumn = DataBinder.Eval(e.Row.DataItem, "yourColumnName").ToString();
更新:
刚注意到您正在使用 ItemDataBound
,这是 DataGrid
的事件,而不是 Gridview
。使用 RowDataBound
事件代替:-
<asp:GridView ID="gridviewALL" runat="server" OnRowDataBound="gridviewALL_RowDataBound">
</asp:GridView>
您的 rowDataBound 事件应如下所示:-
protected void gridviewALL_RowDataBound(object sender, GridViewRowEventArgs e)
{
//your code here
}