如何在网格视图中绑定数据网格中的下拉列表.... [英] How Do I Bind A Drop Down List In Data Grid Not In Gridview....

查看:93
本文介绍了如何在网格视图中绑定数据网格中的下拉列表....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码 aspx 代码

here is my code aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
    
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script src="datepicker/jquery.datepick.js" type="text/javascript"></script>
    <link href="datepicker/jquery.datepick.css" rel="stylesheet" type="text/css" />
    
  <script type="text/javascript">
      $(function () {
          $(".TextBox4").datepick({ dateFormat: 'mm/dd/yyyy', yearRange: 'any', showTrigger: '#calImg' });
      });
  </script>


</head>
<body>
    <form id="form1"  runat="server">
    <div>
        <asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" 

            oneditcommand="DataGrid1_EditCommand1" 

            oncancelcommand="DataGrid1_CancelCommand" 

            ondeletecommand="DataGrid1_DeleteCommand" 

            onupdatecommand="DataGrid1_UpdateCommand" DataKeyField="AdmissionNo" 

            ItemStyle-Width="25" GridLines="None" HorizontalAlign="Center" 

            Width="1024px" CellPadding="4" ForeColor="#333333" 

            onitemcommand="DataGrid1_ItemCommand">

<big>            <<alternatingitemstyle backcolor="White />--%><br mode=" hold=" />            <AlternatingItemStyle BackColor=" white=" /><br mode=" admissionno="><br mode=" href="">abel1=" runat=" server=" Text='<%# Eval(" textbox5=" runat=" true=" Width=" 30px=" Font-Underline=" false="></asp:TextBox><br mode=" first="" name="><br mode=" label2=" runat=" f_name=") %>'></asp:Label><br mode=" textbox6=" runat=" last="" label4=" runat=" l_name=") %>'></asp:Label><br mode=" textbox8=" runat=" date="" of="" birth="><br mode=" label5=" runat=" dob=") %>'></asp:Label><br mode=" textbox4=" runat=" gender="><br mode=" label12=" runat=" radiobuttonlist1=" runat=" radiobuttonlist1_selectedindexchanged=" <br mode=" class="><br mode=" label6=" runat=" ddl1=" runat=" country="><br mode=" label8=" runat=" ddl2_cntry=" runat=" state="><br mode=" label9=" runat=" ddl3=" runat=" mobile="><br mode=" label10=" runat=" textbox2=" runat=" roll="" no="><br mode=" label11=" runat=" roll_no=") %>'></asp:Label><br mode=" textbox3=" runat=" images="><br mode=" image1=" runat=" 40px=" Width=" 47px=" <br mode=" img=") %>' /><br mode=" cancel=" EditText=" edit=" UpdateText=" update="><br mode=" delete=" HeaderText=" center=" VerticalAlign=" middle=" /><br mode=" 10px=" Height=" 5px=" BackColor=" textbox1=" runat=" data="" source="kaushik;database=ankit;" trusted_connection="yes;");<br" mode="hold" />







DataSet ds = new DataSet();
SqlDataAdapter ad = new SqlDataAdapter();
DataTable datat = new DataTable();


string s;

protected void Page_Load(object sender, EventArgs e)

{
    if(!IsPostBack)
    {
        Bind();
    }

}
    
public void  Bind()
{
    s = ("select * from record1");
    ad = new SqlDataAdapter("select * from record1", con);
    ad.Fill(ds);
    //datat=ds.Tables["s"];
    DataGrid1.DataSource = ds;
    DataGrid1.DataBind();
}

protected void DataGrid1_EditCommand1(object source, DataGridCommandEventArgs e)
{
    DataGrid1.EditItemIndex = e.Item.ItemIndex;
    
    Bind();

}
protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
{
    DataGrid1.EditItemIndex = -1;
    Bind();
}
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{

}
protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
    string t1;
    TextBox box;
    box = (TextBox)(e.Item.Cells[1].FindControl("FirstName"));
    t1 = box.Text;
    // var AdmissionNo= DataGrid1.DataKeys(e.)

}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
{


    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        con.Open();
        DropDownList ddl2_cntry = (DropDownList)e.Item.FindControl("ddl2_cntry") as DropDownList;
        DropDownList ddl = new DropDownList();
     
        SqlCommand cmd = new SqlCommand("select * from country", con);
        SqlDataAdapter sd = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        sd.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            con.Close();
            ddl2_cntry.DataSource = dt;
            ddl2_cntry.DataTextField = "countryName";
            ddl2_cntry.DataValueField = "c_id";
            ddl2_cntry.DataBind();
            ddl2_cntry.Items.Insert(0, new ListItem("--select--", "0"));
        }
    }
    }




OBJECT REFRENCE NOT SET TO AN INSTANCE OF AN OBJECT. 
(ddl2_cntry.DataSource = dt;) FOUND IN DIS LINE....

推荐答案

function (){


.TextBox4)。datepick({dateFormat:' mm / dd / yyyy', yearRange:' any',showTrigger:' #calImg'});
});
< / script >


< / head >
< 正文 >
< 表单 id = form1 runat = server >
< div <跨度lass =code-keyword>>
< asp:DataGrid ID = DataGrid1 runat = server AutoGenerateColumns = 错误

oneditcommand=\"DataGrid1_EditCommand1\"

oncancelcommand=\"D ataGrid1_CancelCommand\"

ondeletecommand=\"DataGrid1_DeleteCommand\"

onupdatecommand=\"DataGrid1_UpdateCommand\" DataKeyField=\"AdmissionNo\"

ItemStyle-Width=\"25\" GridLines=\"None\" HorizontalAlign=\"Center\"

Width=\"1024px\" CellPadding=\"4\" ForeColor=\"#333333\"

onitemcommand=\"DataGrid1_ItemCommand\">

<big> <<alternatingitemstyle backcolor=\"White />--%><br mode=\" hold=\" /> <AlternatingItemStyle BackColor=\" white=\" /><br mode=\" admissionno=\"><br mode=\" href=\"\">abel1=\" runat=\" server=\" Text='<%# Eval(\" textbox5=\" runat=\" true=\" Width=\" 30px=\" Font-Underline=\" false=\"></asp:TextBox><br mode=\" first=\"\" name=\"><br mode=\" label2=\" runat=\" f_name=\") %>'></asp:Label><br mode=\" te xtbox6=\" runat=\" last=\"\" label4=\" runat=\" l_name=\") %>'></asp:Label><br mode=\" textbox8=\" runat=\" date=\"\" of=\"\" birth=\"><br mode=\" label5=\" runat=\" dob=\") %>'></asp:Label><br mode=\" textbox4=\" runat=\" gender=\"><br mode=\" label12=\" runat=\" radiobuttonlist1=\" runat=\" radiobuttonlist1_selectedindexchanged=\" <br mode=\" class=\"><br mode=\" label6=\" runat=\" ddl1=\" runat=\" country=\"><br mode=\" label8=\" runat=\" ddl2_cntry=\" runat=\" state=\"><br mode=\" label9=\" runat=\" ddl3=\" runat=\" mobile=\"><br mode=\" label10=\" runat=\" textbox2=\" runat=\" roll=\"\" no=\"><br mode=\" label11=\" runat=\" roll_no=\") %>'></asp:Label><br mode=\" textbox3=\" runat=\" images=\"><br mode=\" image1=\" runat=\" 40px=\" Width=\" 47px=\" <br mode=\" img=\") %>' /><br mode=\" cancel=\" EditText=\" edit=\" UpdateText=\" update=\"><br mode=\" delete=\" HeaderText=\" center=\" VerticalAlign=\" middle=\" /><br mode=\" 10px=\" Height=\" 5px=\" BackColor=\" textbox1=\" runat=\" data=\"\" source=\"kaushik;database=ankit;\" trusted_connection=\"yes;\");< ;br\" mode=\"hold\" />
(".TextBox4").datepick({ dateFormat: 'mm/dd/yyyy', yearRange: 'any', showTrigger: '#calImg' }); }); </script> </head> <body> <form id="form1" runat="server"> <div> <asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" oneditcommand="DataGrid1_EditCommand1" oncancelcommand="DataGrid1_CancelCommand" ondeletecommand="DataGrid1_DeleteCommand" onupdatecommand="DataGrid1_UpdateCommand" DataKeyField="AdmissionNo" ItemStyle-Width="25" GridLines="None" HorizontalAlign="Center" Width="1024px" CellPadding="4" ForeColor="#333333" onitemcommand="DataGrid1_ItemCommand"> <big> <<alternatingitemstyle backcolor="White />--%><br mode=" hold=" /> <AlternatingItemStyle BackColor=" white=" /><br mode=" admissionno="><br mode=" href="">abel1=" runat=" server=" Text='<%# Eval(" textbox5=" runat=" true=" Width=" 30px=" Font-Underline=" false="></asp:TextBox><br mode=" first="" name="><br mode=" label2=" runat=" f_name=") %>'></asp:Label><br mode=" textbox6=" runat=" last="" label4=" runat=" l_name=") %>'></asp:Label><br mode=" textbox8=" runat=" date="" of="" birth="><br mode=" label5=" runat=" dob=") %>'></asp:Label><br mode=" textbox4=" runat=" gender="><br mode=" label12=" runat=" radiobuttonlist1=" runat=" radiobuttonlist1_selectedindexchanged=" <br mode=" class="><br mode=" label6=" runat=" ddl1=" runat=" country="><br mode=" label8=" runat=" ddl2_cntry=" runat=" state="><br mode=" label9=" runat=" ddl3=" runat=" mobile="><br mode=" label10=" runat=" textbox2=" runat=" roll="" no="><br mode=" label11=" runat=" roll_no=") %>'></asp:Label><br mode=" textbox3=" runat=" images="><br mode=" image1=" runat=" 40px=" Width=" 47px=" <br mode=" img=") %>' /><br mode=" cancel=" EditText=" edit=" UpdateText=" update="><br mode=" delete=" HeaderText=" center=" VerticalAlign=" middle=" /><br mode=" 10px=" Height=" 5px=" BackColor=" textbox1=" runat=" data="" source="kaushik;database=ankit;" trusted_connection="yes;");<br" mode="hold" />







DataSet ds = new DataSet();
SqlDataAdapter ad = new SqlDataAdapter();
DataTable datat = new DataTable();


string s;

protected void Page_Load(object sender, EventArgs e)

{
    if(!IsPostBack)
    {
        Bind();
    }

}
    
public void  Bind()
{
    s = ("select * from record1");
    ad = new SqlDataAdapter("select * from record1", con);
    ad.Fill(ds);
    //datat=ds.Tables["s"];
    DataGrid1.DataSource = ds;
    DataGrid1.DataBind();
}

protected void DataGrid1_EditCommand1(object source, DataGridCommandEventArgs e)
{
    DataGrid1.EditItemIndex = e.Item.ItemIndex;
    
    Bind();

}
protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
{
    DataGrid1.EditItemIndex = -1;
    Bind();
}
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{

}
protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
    string t1;
    TextBox box;
    box = (TextBox)(e.Item.Cells[1].FindControl("FirstName"));
    t1 = box.Text;
    // var AdmissionNo= DataGrid1.DataKeys(e.)

}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
{


    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        con.Open();
        DropDownList ddl2_cntry = (DropDownList)e.Item.FindControl("ddl2_cntry") as DropDownList;
        DropDownList ddl = new DropDownList();
     
        SqlCommand cmd = new SqlCommand("select * from country", con);
        SqlDataAdapter sd = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        sd.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            con.Close();
            ddl2_cntry.DataSource = dt;
            ddl2_cntry.DataTextField = "countryName";
            ddl2_cntry.DataValueField = "c_id";
            ddl2_cntry.DataBind();
            ddl2_cntry.Items.Insert(0, new ListItem("--select--", "0"));
        }
    }
    }




OBJECT REFRENCE NOT SET TO AN INSTANCE OF AN OBJECT. 
(ddl2_cntry.DataSource = dt;) FOUND IN DIS LINE....


When you have a control in a datagrid, if you want to bind it, you need to use the findControl method in the datagrid.ItemDataBound handler example:



When you have a control in a datagrid, if you want to bind it, you need to use the findControl method in the datagrid.ItemDataBound handler example:

protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{
  DropDownList lst = (DropDownList) e.Item.findControl("ddl2_cntry");
  lst.DataSource = x; //whatever datasource you need will be replaced by 'x'
  lst.DataBind();
}


这篇关于如何在网格视图中绑定数据网格中的下拉列表....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆