如何在gridview中的下拉列表中添加项目列表 [英] How to add list of items in dropdownlist in gridview

查看:63
本文介绍了如何在gridview中的下拉列表中添加项目列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当网页正在运行时,网格视图正在显示来自数据库的数据在我们点击编辑按钮后,下拉列表应该是可编辑的,而选择是应该显示城市列表。这是我想要的任何一个帮助我..





when web page is running grid view is displaying data from database on bind after we click on edit button the dropdown should be editable and on select is should display a list of cities .this is what i want can any one help me on this..


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowcancelingEdit" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="userid" OnRowEditing="GridView1_RowEditing" >
           <Columns>
               <asp:BoundField DataField="userid" HeaderText="userid" ReadOnly="true" />
               <asp:TemplateField HeaderText="name">
               <ItemTemplate>
           <%# Eval("name")%>
         </ItemTemplate>
         <EditItemTemplate>
           <asp:TextBox runat="server" ID="txtname" Text='<%# Bind("name")%>' />
         </EditItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="age">
               <ItemTemplate>
             <%# Eval("age")%>
         </ItemTemplate>
         <EditItemTemplate>
             <asp:TextBox runat="server" ID="txtage" Text='<%# Bind("age")%>' />
         </EditItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="City" SortExpression="City">
                   <EditItemTemplate>
                       <asp:DropDownList ID="DropDownList1" runat="server"

                           DataSourceID="SqlDataSource2" DataTextField="City" DataValueField="City"

                           SelectedValue='<%# Bind("city") %>' >
                       </asp:DropDownList>
                   </EditItemTemplate>
                   <ItemTemplate>
                       <asp:Label ID="Label1" runat="server" Text='<%# Bind("city") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:CommandField ShowEditButton="True" ButtonType="Button" />
           </Columns>
       </asp:GridView>

       <br />

   </div>
   <asp:SqlDataSource ID="SqlDataSource2" runat="server"

       ConnectionString="Server=ALOK\SQLEXPRESS; Database=Employee; uid=sa; pwd=1234;"

       SelectCommand="SELECT DISTINCT [City] FROM [employ]"></asp:SqlDataSource>
   <br />
   <br />
   </form>

推荐答案

引用:

是的,在数据​​库中我只有一个城市,但我想添加许多城市如何实现

yes in database i have only one city but i want to add many cities how to achive that

从您的评论中,很明显您在数据库表中只有一个城市。



如果你想要显示更多城市,然后只需在数据库管理软件中手动添加该表中的更多城市,或者构建一些aspx页面以将城市插入该表。



之后,它将自动反映在 GridView DropDownList

From your comment, it is quite clear that the you have only one city in Database table.

If you want to show more cities, then just add more cities in that table either manually in Database Management Software or build some aspx page to insert cities to that table.

After that, it will automatically reflect on GridView DropDownList.


在源视图中

In source view
<asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown"></asp:dropdownlist>

代码背后

在行编辑活动中

In code behind
In row editing event

DropdownList ddl=(DropdownList)GridView1.Rows[e.RowIndex].FindControl("DropdownList");
//take city name in dataset ds and bind that data to GridView
//to make selection in dropdownlist
ddl.SelectedItem=ddl.Items.FindByText("city");


这是我的标记

--------------

This was my markup
--------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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.10.2/jquery.min.js">

</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>


        <br />

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowcancelingEdit" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="userid" OnRowEditing="GridView1_RowEditing" >
            <Columns>
                <asp:BoundField DataField="userid" HeaderText="userid" ReadOnly="true" />
                <asp:TemplateField HeaderText="name">
                <ItemTemplate>
            <%# Eval("name")%>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtname" Text='<%# Bind("name")%>' />
          </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="age">
                <ItemTemplate>
              <%# Eval("age")%>
          </ItemTemplate>
          <EditItemTemplate>
              <asp:TextBox runat="server" ID="txtage" Text='<%# Bind("age")%>' />
          </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="city" SortExpression="city">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1"  runat="server" AutoPostBack="true"

                             DataTextField="city" DataValueField="city">
                            <asp:ListItem>Select</asp:ListItem>
                            <asp:ListItem>Kanpur</asp:ListItem>
                            <asp:ListItem>Lucknow</asp:ListItem>
                            <asp:ListItem>Mumbai</asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("city") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True" ButtonType="Button" />
            </Columns>
        </asp:GridView>

        <br />

    </div>
     <br />
    <br />
    </form>
</body>
</html>











-------------------------------------------



This was my code



-------------------






-------------------------------------------

This was my code

-------------------

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

public partial class _Default : System.Web.UI.Page
{

      protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
          
            BindData();
        }

    }

    private void BindData()
    {
        string constr = @"Server=ALOK\SQLEXPRESS; Database=Employee; uid=sa; pwd=1234;";
        string query = "SELECT userid, name, age,city FROM employ";
        SqlDataAdapter da = new SqlDataAdapter(query, constr);
        DataTable table = new DataTable();
        da.Fill(table);
        GridView1.DataSource = table;
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
                 
        BindData();
    }

    protected void GridView1_RowcancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        e.Cancel = true;
        GridView1.EditIndex = -1;
        BindData();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        var name = e.NewValues["name"] as string;
        var age = (string)e.NewValues["age"];
     
        DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropdownList1");
       
        string city = ddl.SelectedItem.Value;
        string userid = (string)e.Keys[0];
                       
       UpdateProduct(userid, name, age,city);

    }
    private void UpdateProduct(string userid, string name, string age, string city)
    {
        try
        {
            string constr = @"Server=ALOK\SQLEXPRESS; Database=Employee; uid=sa; pwd=1234;";
            string query = "UPDATE employ SET name = @name, age = @age , city=@city WHERE userid = @userid";


            SqlConnection con = new SqlConnection(constr);
            SqlCommand com = new SqlCommand(query, con);


            com.Parameters.Add("@name", SqlDbType.NVarChar).Value = name;
            com.Parameters.Add("@age", SqlDbType.Int).Value =Convert.ToInt32(age);
            com.Parameters.Add("@city", SqlDbType.NVarChar).Value = city;
            com.Parameters.Add("@userid", SqlDbType.NVarChar).Value = userid;


            con.Open();
            com.ExecuteNonQuery();
            con.Close();


            GridView1.EditIndex = -1;
            BindData();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }  

   
  
 }


这篇关于如何在gridview中的下拉列表中添加项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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