2015-10-14 73 views
2

如何在代碼後面的DataTable中設置此下拉列表?用CodeBehind中的DataTable填充DropDownList

<asp:GridView ID="gvTemplateFields" 
       runat="server" 
       CssClass="grid" 
       AutoGenerateColumns="false" 

    <Columns> 
     <asp:TemplateField HeaderText="Estado" ItemStyle-Width="50px"> 
      <ItemTemplate> 
       <asp:DropDownList ID="RiskWorkDropDownList" runat="server"> 
        <asp:ListItem Value="1">Pendiente</asp:ListItem> 
        <asp:ListItem>Atendido</asp:ListItem> 
       </asp:DropDownList> 
      </ItemTemplate> 
     </asp:TemplateField>     
    </Columns> 

    <EmptyDataTemplate>No off-site links found.</EmptyDataTemplate> 

</asp:GridView> 

背後的代碼:

public int SWMSTemplateId; 
public DropDownList RiskWorkDropDownList; 

protected void Page_Load(object sender, EventArgs e) 
{ 
    SWMSTemplateId = int.Parse(Request.QueryString["templateid"]); 

    DataTable templateFields = SWMSField.GetTemplateFields(SWMSTemplateId); 

    RiskWorkDropDownList.DataSource = templateFields; 
    RiskWorkDropDownList.DataBind(); 

} 

錯誤:

System.NullReferenceException: Object reference not set to an instance of an object. 

RiskWorkDropDownList是空

RiskWorkDropDownList.DataSource = templateFields; 

我試圖得到它的工作就像它確實這個問題/答案:

DropdownList DataSource

+1

顯示你有什麼到目前爲止您的企圖在後面的代碼,所以,不管是誰回答不必從頭開始寫。 – mason

+0

哪條線給你錯誤?什麼對象是null?你知道什麼是NullReferenceException嗎? – mason

回答

0

一些簡單的像這樣應該可以幫助您:

C#

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace WebApplication1 
{ 
    public partial class WebForm1 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      DataTable theTable = new DataTable(); 
      theTable.Columns.Add("Names", typeof(string)); 
      theTable.Rows.Add("Name1"); 
      theTable.Rows.Add("Name2"); 
      theTable.Rows.Add("Name3"); 

      for (int i = 0; i < theTable.Rows.Count; i++) 
      { 
       string theValue = theTable.Rows[i].ItemArray[0].ToString(); 
       DropDownList1.Items.Add(theValue); 
      } 

     } 
    } 
} 

ASP

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList> 
    </div> 
    </form> 
</body> 
</html> 
+0

我不能只使用DataBind()之類的東西嗎? –

+0

不值得投票,因爲你沒有要求它。不要在沒有提供信息的情況下爲幫助你的人投票。 – mwilson

+0

我沒有倒下你 –

0

你可能想每一個新的DropDownList實例綁定,當一個新行被建造。那麼你可能想要GridView.RowCreated事件。

void gvTemplateFields_RowCreated(Object sender, GridViewRowEventArgs e) 
{ 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     (DropDownList)riskWorkDropDownList = (DropDownList)e.Row.FindControl("RiskWorkDropDownList"); 

     riskWorkDropDownList.DataSource = dataTable; //Your data table 
     riskWorkDropDownList.DataBind(); 
    } 
} 
0

我假設你正在使用SQLEXPRESS服務器,並試圖從數據庫表中獲取數據到下拉列表,請 試試這個。

var Cn = new System.Data.SqlClient.SqlConnection(); 
Cn.ConnectionString = "Server=.\\SqlExpress;Database=YourDatabasename;Trusted_Connection=True"; 
Cn.Open(); 
var Cm = Cn.CreateCommand(); 
Cm.CommandText = string.Format(@"Select * From DataTablename"); 
var Dr = Cm.ExecuteReader(); 

//add all data from database to dropdownlist 
while (Dr.Read()) 
    { 
     RiskWorkDropDownList.Items.Add(new ListItem(Dr.GetValue(1/*your table column*/).ToString())); 
    } 

Cn.Close();