2014-03-27 54 views
0

我有一个Designation表:的DropDownList不能设置DataTextField上GridViewSelectedIndexChangedEvent

CREATE TABLE TblDesignation 
(
    [DesgId] [int] IDENTITY(1,1) NOT NULL, 
    [Designation] [nvarchar](50) NOT NULL, 
CONSTRAINT [PK_TblDesignation] PRIMARY KEY CLUSTERED 
(
    [DesgId] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

Employee.aspx页面代码:

<div id="divView" align="center" runat="server" > 
    <asp:Panel ID="Panel1" runat="server"> 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
      CellPadding="4" ForeColor="#333333" GridLines="None" 
      DataSourceID="SqlDataSource1" 
      onselectedindexchanged="GridView1_SelectedIndexChanged"> 
      <AlternatingRowStyle BackColor="White" /> 
      <Columns> 
       <asp:CommandField ShowSelectButton="True" /> 
       <asp:BoundField DataField="EmpId" HeaderText="EMP ID" /> 
       <asp:BoundField DataField="EmpFirstName" HeaderText="FIRST NAME" /> 
       <asp:BoundField DataField="EmpMiddleName" HeaderText="MIDDLE NAME" /> 
       <asp:BoundField DataField="EmpLastName" HeaderText="LAST NAME" /> 
       <asp:BoundField DataField="EmpGender" HeaderText="GENDER" /> 
       <asp:BoundField DataField="EmpDesignation" HeaderText="DESIGNATION" /> 
       <asp:BoundField DataField="EmpAddress" HeaderText="ADDRESS" /> 
       <asp:BoundField DataField="EmpEmailId" HeaderText="EMAIL ID" /> 
       <asp:BoundField DataField="EmpContactNo" HeaderText="CONTACT" /> 
       <asp:BoundField DataField="EmpDOB" HeaderText="D.O.B" dataformatstring="{0:MM/dd/yyyy}"/> 
      </Columns> 
      <EditRowStyle BackColor="#7C6F57" /> 
      <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> 
      <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> 
      <RowStyle BackColor="#E3EAEB" /> 
      <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> 
      <SortedAscendingCellStyle BackColor="#F8FAFA" /> 
      <SortedAscendingHeaderStyle BackColor="#246B61" /> 
      <SortedDescendingCellStyle BackColor="#D4DFE1" /> 
      <SortedDescendingHeaderStyle BackColor="#15524A" /> 
     </asp:GridView> 

     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="<%$ ConnectionStrings:MSAMBConnectionString %>" 
      SelectCommand="SELECT * FROM [TblEmployee]"></asp:SqlDataSource> 

    </asp:Panel> 
    <asp:Button ID="btnAddNew" runat="server" Text="ADD NEW EMPLOYEE" 
     onclick="btnAddNew_Click" /> 
</div> 
<div id="divForm" runat="server" style="margin-top:4%;"> 
<table border="2" align="center"> 
<tr> 
<td> 
    <asp:Label ID="LblEmpId" runat="server" Text="EMP ID"></asp:Label> 
</td> 
<td> 
    <asp:TextBox ID="txtEmpId" runat="server" Enabled="False"></asp:TextBox> 
</td> 
</tr> 
<tr> 
<td> 
    <asp:Label ID="LblEmpFirstName" runat="server" Text="FIRST NAME : "></asp:Label> 
</td> 
<td> 
    <asp:TextBox ID="txtEmpFirstName" runat="server"></asp:TextBox> 
</td> 
</tr> 

<tr> 
<td> 
    <asp:Label ID="LblEmpMiddleName" runat="server" Text="MIDDLE NAME : "></asp:Label> 
</td> 
<td> 
    <asp:TextBox ID="txtEmpMiddleName" runat="server"></asp:TextBox> 
</td> 
</tr> 

<tr> 
<td> 
    <asp:Label ID="LblEmpLastName" runat="server" Text="LAST NAME : "></asp:Label> 
</td> 
<td> 
    <asp:TextBox ID="txtEmpLastName" runat="server"></asp:TextBox> 
</td> 
</tr> 

<tr> 
<td> 
    <asp:Label ID="LblGender" runat="server" Text="GENDER : "></asp:Label> 
</td> 
<td> 
    <asp:RadioButton ID="rbMale" runat="server" Text="Male" GroupName="Gender" /> 
    <asp:RadioButton ID="rbFemale" runat="server" Text="Female" GroupName="Gender" /> 
</td> 
</tr> 


<tr> 
<td> 
    <asp:Label ID="LblEmpDesignation" runat="server" Text="DESIGNATION : "></asp:Label> 
</td> 
<td> 
    <asp:DropDownList ID="ddrDesgn" runat="server" DataSourceID="SqlDataSource2" 
     DataTextField="Designation" DataValueField="DesgId" 
     onselectedindexchanged="ddrDesgn_SelectedIndexChanged" AutoPostBack="True"> 
    </asp:DropDownList> 

    **<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
     ConnectionString="<%$ ConnectionStrings:MSAMBConnectionString3 %> 
    SelectCommand="SELECT * FROM [TblDesignation]"></asp:SqlDataSource>** 

</td> 
</tr> 



<tr> 
<td> 
    <asp:Label ID="LblEmpAddress" runat="server" Text="ADDRESS : "></asp:Label> 
</td> 
<td> 
    <asp:TextBox ID="txtEmpAddress" runat="server"></asp:TextBox> 
</td> 
</tr> 

<tr> 
<td> 
    <asp:Label ID="LblEmpEmailId" runat="server" Text="EMAIL ID : "></asp:Label> 
</td> 
<td> 
    <asp:TextBox ID="txtEmpEmailId" runat="server"></asp:TextBox> 
</td> 
</tr> 

<tr> 
<td> 
    <asp:Label ID="LblEmpContactNo" runat="server" Text="CONTACT NO : "></asp:Label> 
</td> 
<td> 
    <asp:TextBox ID="txtEmpContactNo" runat="server"></asp:TextBox> 
</td> 
</tr> 


<tr> 
<td> 
    <asp:Label ID="LblEmpDOB" runat="server" Text="DOB : "></asp:Label> 
</td> 
<td> 
    <asp:TextBox ID="txtEmpDOB" runat="server"></asp:TextBox> 
    <asp:CalendarExtender ID="txtEmpDOB_CalendarExtender" runat="server" 
     Enabled="True" TargetControlID="txtEmpDOB"> 
    </asp:CalendarExtender> 
</td> 
</tr> 
    <tr> 
    <td colspan="1" align="center"> 
     <asp:Button ID="btnSubmit" runat="server" Text="SAVE" 
      onclick="btnSubmit_Click" /></td> 
     <td><asp:Button ID="btnUpdate" runat="server" Text="UPDATE" 
       onclick="btnUpdate_Click" /> 
    </td> 
    </tr> 
    <tr> 
    <td colspan="2"> 
    <asp:Label ID="lblMsg" runat="server" Text=""></asp:Label> 
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Employee.aspx">BACK</asp:HyperLink> 
    </td> 

    </tr> 


</table> 

</div> 

Employee.aspx.cs C#代码:

On protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (GridView1.SelectedRow.Cells[5].Text == "Male") 
     { 
      rbMale.Checked = true; 
     } 
     else { 
      rbFemale.Checked = true; 
     } 
     txtEmpId.Text = GridView1.SelectedRow.Cells[1].Text; 
     txtEmpFirstName.Text = GridView1.SelectedRow.Cells[2].Text; 
     txtEmpMiddleName.Text = GridView1.SelectedRow.Cells[3].Text; 
     txtEmpLastName.Text = GridView1.SelectedRow.Cells[4].Text; 
     ddrDesgn.DataTextField = GridView1.SelectedRow.Cells[6].Text; 
     txtEmpAddress.Text = GridView1.SelectedRow.Cells[7].Text; 
     txtEmpEmailId.Text = GridView1.SelectedRow.Cells[8].Text; 
     txtEmpContactNo.Text = GridView1.SelectedRow.Cells[9].Text; 
     txtEmpDOB.Text = GridView1.SelectedRow.Cells[10].Text; 
     divView.Visible = false; 
     divForm.Visible = true; 

    } 

它给我错误:

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'MD'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.  
Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'MD'. 

源错误:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

回答

0

似乎网格认为你试图绑定到一个名为“MD”时,没有这样的事情在源数据中存在的东西,虽然我不能看到在你的代码,尝试这个去视觉工作室,Server Exolorer(Ctrl-Alt-S)创建一个连接到您的数据库,导航到您的表,确保您的ASP页面上的GUI模式,然后将表拖到页面,VS会自动生成SQL和Gridview,从那里你应该能够使用数据源或网格添加/删除列,直到你得到你需要的。

+0

在CS代码中我有这样的连接字符串.. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [“con”] .ConnectionString); 和ASPX PAGE dropdoenlist绑定像这样的表... –

+0

你的问题说我有一个指定表:但是你的代码是在GridView中做DataSourceID =“SqlDataSource1”,你的SQL代码将选择* FROM [TblEmployee ]在数据源1,进一步向下你有SqlDataSource2其中包含SELECT * FROM [TblDesignation],我认为你需要改变数据源,更改Gridview1的ID属性为DataSourceID =“SqlDataSource2” – saj

+0

好吧,但在gridview中,我可以显示来自TblEmployee的所有记录以及在下拉列表中显示来自TblDesignation表的指定... –

相关问题