如何将Database字段值绑定到gridview内的隐藏字段

时间:2011-02-18 07:09:27

标签: asp.net mysql gridview hiddenfield

我使用以下命令将表中的字段绑定到gridview内的隐藏字段,但我收到错误,因为System.Data.DataRowView'不包含名为“AccountType”的属性。

这就是我指定的方式

<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>

2 个答案:

答案 0 :(得分:0)

您应该使用以下表示法:&lt;%#DataBinder.Eval(Container.DataItem,“AccountType”)%&gt;

答案 1 :(得分:0)

正如Muhammed Akhtar所说,您的原始数据源不包含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字段。如果你没有在你原来的那个字段中包含这个字段,它就不会被包含在你得到的数据源中,这反过来又意味着你无法绑定它

希望有所帮助

戴夫