未在 GridView 中正确设置数据
Not properly set Data in GridView
我正在尝试从数据库中检索数据并在 gridview 中显示。为了创建 Header,我正在使用 HeaderTemplet 标签。但在 gridview 中,员工 ID 的第一列始终为空白。
我的代码如下aspx页面:
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" Width="99%" GridLines="Both" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<tr>
<th style="padding: 2.5px; width: 10%;" >eid</th>
<th style="padding: 2.5px; width: 55%;" >First Name</th>
<th style="padding: 2.5px;" >Last Name</th>
</HeaderTemplate>
</asp:TemplateField>
<asp:BoundField DataField="idemp" />
<asp:BoundField DataField="fname" />
<asp:BoundField DataField="lname" />
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" Width="99%" GridLines="Both" AutoGenerateColumns="false" CssClass="ChildGrid">
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
隐藏代码:
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Populate the GridView.
bindGridview();
}
}
public void bindGridview()
{
DataTable dt = new DataTable();
string constr = @"server=127.0.0.1;user id=root;pwd=n0711p2010p;database=emp";
using (MySqlConnection con = new MySqlConnection(constr))
{
MySqlCommand cmd = new MySqlCommand("select idemp,fname,lname from emp", con);
con.Open();
MySqlDataReader dtreader = cmd.ExecuteReader();
dt.Load(dtreader);
if (dt.Rows.Count >0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
}
}
//RowDataBound Event
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Checking the RowType of the Row
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = new DataTable();
string constr = @"server=127.0.0.1;user id=root;pwd=n0711p2010p;database=emp";
using (MySqlConnection con = new MySqlConnection(constr))
{
con.Open();
GridView child_gridview = (GridView)e.Row.FindControl("GridView2");
String CountryId = (e.Row.RowIndex+1).ToString();
MySqlCommand cmd = new MySqlCommand("select salary,post from emp where idemp="+CountryId, con);
MySqlDataReader dtreader = cmd.ExecuteReader();
dt.Load(dtreader);
if (dt.Rows.Count > 0)
{
child_gridview.DataSource = dt;
child_gridview.DataBind();
}
}
}
}
}
}
输出如下:
gridview 的第一列总是 empty.when 使用 HeaderTemplet 标签。
我想达到这样的效果。
您似乎可以很好地检索数据,但显示不正确。
您错误地使用 TemplateField
来定义 headers。
您可以简单地执行以下操作:
<asp:GridView ID="GridView1" runat="server" Width="99%" GridLines="Both" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="idemp" headertext="Employee ID" />
<asp:BoundField DataField="fname" headertext="First Name" />
<asp:BoundField DataField="lname" headertext="Last Name" />
</Columns>
</asp:GridView>
如您所见,Header 名称是在绑定列中定义的。
祝您愉快,如果还有问题请告诉我
如果您希望嵌套的 GridView 跨越多列,则必须在父 GridView 的 RowDataBound 中执行此操作。在那里你可以设置 colspan 并删除最后 2 个单元格。
所以如果你有一个 GridView
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<%# Eval("Id") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Saldy">
<ItemTemplate>
<asp:GridView ID="NestedGrid" runat="server" AutoGenerateColumns="true">
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
</asp:TemplateField>
</Columns>
</asp:GridView>
以及 RowDataBound 事件
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a normal datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the row back to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
//use findcontrol to locate the nested gridview
GridView gv = e.Row.FindControl("NestedGrid") as GridView;
//bind data to the nested grid
gv.DataSource = source;
gv.DataBind();
//set the column span to 3 on the cell that has the nested gridview
e.Row.Cells[2].ColumnSpan = 3;
//hide the last 2 cells
e.Row.Cells[3].Visible = false;
e.Row.Cells[4].Visible = false;
}
}
唯一的问题是父子单元格的宽度不匹配,因此您可能需要为这些列设置固定宽度。
我正在尝试从数据库中检索数据并在 gridview 中显示。为了创建 Header,我正在使用 HeaderTemplet 标签。但在 gridview 中,员工 ID 的第一列始终为空白。
我的代码如下aspx页面:
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" Width="99%" GridLines="Both" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<tr>
<th style="padding: 2.5px; width: 10%;" >eid</th>
<th style="padding: 2.5px; width: 55%;" >First Name</th>
<th style="padding: 2.5px;" >Last Name</th>
</HeaderTemplate>
</asp:TemplateField>
<asp:BoundField DataField="idemp" />
<asp:BoundField DataField="fname" />
<asp:BoundField DataField="lname" />
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" Width="99%" GridLines="Both" AutoGenerateColumns="false" CssClass="ChildGrid">
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
隐藏代码:
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Populate the GridView.
bindGridview();
}
}
public void bindGridview()
{
DataTable dt = new DataTable();
string constr = @"server=127.0.0.1;user id=root;pwd=n0711p2010p;database=emp";
using (MySqlConnection con = new MySqlConnection(constr))
{
MySqlCommand cmd = new MySqlCommand("select idemp,fname,lname from emp", con);
con.Open();
MySqlDataReader dtreader = cmd.ExecuteReader();
dt.Load(dtreader);
if (dt.Rows.Count >0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
}
}
//RowDataBound Event
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Checking the RowType of the Row
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = new DataTable();
string constr = @"server=127.0.0.1;user id=root;pwd=n0711p2010p;database=emp";
using (MySqlConnection con = new MySqlConnection(constr))
{
con.Open();
GridView child_gridview = (GridView)e.Row.FindControl("GridView2");
String CountryId = (e.Row.RowIndex+1).ToString();
MySqlCommand cmd = new MySqlCommand("select salary,post from emp where idemp="+CountryId, con);
MySqlDataReader dtreader = cmd.ExecuteReader();
dt.Load(dtreader);
if (dt.Rows.Count > 0)
{
child_gridview.DataSource = dt;
child_gridview.DataBind();
}
}
}
}
}
}
输出如下:
gridview 的第一列总是 empty.when 使用 HeaderTemplet 标签。
我想达到这样的效果。
您似乎可以很好地检索数据,但显示不正确。
您错误地使用 TemplateField
来定义 headers。
您可以简单地执行以下操作:
<asp:GridView ID="GridView1" runat="server" Width="99%" GridLines="Both" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="idemp" headertext="Employee ID" />
<asp:BoundField DataField="fname" headertext="First Name" />
<asp:BoundField DataField="lname" headertext="Last Name" />
</Columns>
</asp:GridView>
如您所见,Header 名称是在绑定列中定义的。
祝您愉快,如果还有问题请告诉我
如果您希望嵌套的 GridView 跨越多列,则必须在父 GridView 的 RowDataBound 中执行此操作。在那里你可以设置 colspan 并删除最后 2 个单元格。
所以如果你有一个 GridView
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<%# Eval("Id") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Saldy">
<ItemTemplate>
<asp:GridView ID="NestedGrid" runat="server" AutoGenerateColumns="true">
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
</asp:TemplateField>
</Columns>
</asp:GridView>
以及 RowDataBound 事件
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a normal datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the row back to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
//use findcontrol to locate the nested gridview
GridView gv = e.Row.FindControl("NestedGrid") as GridView;
//bind data to the nested grid
gv.DataSource = source;
gv.DataBind();
//set the column span to 3 on the cell that has the nested gridview
e.Row.Cells[2].ColumnSpan = 3;
//hide the last 2 cells
e.Row.Cells[3].Visible = false;
e.Row.Cells[4].Visible = false;
}
}
唯一的问题是父子单元格的宽度不匹配,因此您可能需要为这些列设置固定宽度。