我如何绑定数据库字段值到GridView中的隐藏字段
问题描述:
我使用以下方法将表中的字段绑定到gridview中的隐藏字段,但我得到的错误是System.Data.DataRowView'does不包含名称为“AccountType”的属性。我如何绑定数据库字段值到GridView中的隐藏字段
我这是怎么分配的
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdnAccntType" runat="Server" Value='<%#Eval("AccountType") %>' />
</ItemTemplate>
</asp:TemplateField>
它是正确的还是我做任何修正?
我的存储过程:
CREATE DEFINER=`root`@`%` PROCEDURE `uspGetEmployeeBankDate`(_EmpID INT(11),
_BankTypeID varchar(10),_AccountType varchar(10))
BEGIN
select EmpID,BankTypeID,DATE_FORMAT(StartDate, '%Y-%m-%d')
as StartDate,DATE_FORMAT(EndDate, '%Y-%m-%d') as EndDate
from tblemployeebankdata where EmpID=_EmpID and BankTypeID=_BankTypeID
and AccountType=_AccountType;
END
示例代码
if (mlocal_ds.Tables[0].Rows.Count != 0)
{
foreach (DataRow drRow in mlocal_ds.Tables[0].Rows)
{
if (drRow["BankTypeID"].ToString() == "DbalC")
{
pnlGrid.Visible = false;
grdBank.Visible = true;
//grdData.Visible = false;
strEmpID = HiddenField1.Value;
string AccntType = drRow["AccountType"].ToString();
string strBankTypeID = drRow["BankTypeID"].ToString();
string mlocal_strStoredProcName = StoredProcNames.tblEmployeeBankdetails_uspEmployeeBankdetailsDate;
mlocal_ds = new DataSet();
oEmployee.SelectId(out mlocal_ds, mlocal_strStoredProcName, strEmpID, strBankTypeID,AccntType); // here the stored procedure is executed
grdBank.DataSource = mlocal_ds;
grdBank.DataBind();
pnlChckAcc.Visible = false;
pnlPrimary.Visible = false;
pnlSecond.Visible = false;
pnlSecondary.Visible = false;
pnlFirst.Visible = false;
pnlEditInfo.Visible = false;
break;
}
我的网格视图
<asp:GridView ID="grdBank" runat="server" AutoGenerateColumns="False" CellPadding="4"
CssClass="grid" ForeColor="#333333" GridLines="None" Width="349px" Visible="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="rdbtnBank" runat="server" AutoPostBack="true" OnCheckedChanged="rdbtnBank_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdnAccntType" runat="Server" Value='<%# DataBinder.Eval(Container.DataItem, "AccountType") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmpID" HeaderText="EmpID">
<ItemStyle CssClass="intitalitefname" />
</asp:BoundField>
<asp:BoundField DataField="BankTypeID" HeaderText="BankTypeID">
<ItemStyle CssClass="intitalitefname" />
</asp:BoundField>
<asp:BoundField DataField="StartDate" HeaderText="Start Date">
<ItemStyle CssClass="intitalitefname" />
</asp:BoundField>
<asp:BoundField DataField="EndDate" HeaderText="End Date">
<ItemStyle CssClass="intitalitefname" />
</asp:BoundField>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EmptyDataTemplate>
nodata to display
</EmptyDataTemplate>
</asp:GridView>
答
正如穆罕默德·阿赫塔尔说你原来的数据源不包括ACCOUNTTYPE领域,所以你不能这样绑定到任何
你需要改变你的存储过程来:
CREATE DEFINER=`root`@`%` PROCEDURE `uspGetEmployeeBankDate`(_EmpID INT(11),
_BankTypeID varchar(10),_AccountType varchar(10))
BEGIN
select AccountType, EmpID,BankTypeID,DATE_FORMAT(StartDate, '%Y-%m-%d')
as StartDate,DATE_FORMAT(EndDate, '%Y-%m-%d') as EndDate
from tblemployeebankdata where EmpID=_EmpID and BankTypeID=_BankTypeID
and AccountType=_AccountType;
END
的我修改在你的“select”的开头是正确的,它现在包括AccountType字段。如果您在原始不包括此字段中选择它不会被包含在你回来的数据源,这反过来又意味着你不能将其绑定
希望帮助
戴夫
我用于尝试使用gridview做这样的事情,但决定使用stringbuilder和linq to sql动态构建我自己的表更快更实用。 – 2011-02-18 07:14:35