Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > ASP.NET จะแสดงข้อมูลที่เป็น DropDownList บนใน GridView ทำไงครับ



 

ASP.NET จะแสดงข้อมูลที่เป็น DropDownList บนใน GridView ทำไงครับ

 



Topic : 087985



โพสกระทู้ ( 231 )
บทความ ( 0 )



สถานะออฟไลน์




จะแสดงข้อมูลที่เป็น dropdownlist บนใน gridview ทำไงครับ
โค้ดของผมประมานนี้ครับ

Code (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AdminGridView.aspx.cs" Inherits="WebApp.AdminGridView" %>

<!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>
    <style type="text/css">
        .style1
        {
            font-size: xx-large;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <strong><span class="style1">Admin</span></strong><br />
        <br />
        <asp:GridView ID="myGridView" runat="server"
        AutoGenerateColumns="False" 
	ShowFooter="True" 
	DataKeyNames="UserName" 
	OnRowCancelingEdit="modCancelCommand" 
	OnRowCommand="myGridView_RowCommand" 
	OnRowDeleting="modDeleteCommand" 
	OnRowEditing="modEditCommand" 
	OnRowUpdating="modUpdateCommand" BackColor="White" BorderColor="#CCCCCC" 
        BorderStyle="None" BorderWidth="1px" CellPadding="3">

	<Columns>
	<asp:TemplateField HeaderText="UserName">
		<ItemTemplate>
			<asp:Label id="lblUserName" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UserName") %>'></asp:Label>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:TextBox id="txtEditUserName" size="5" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UserName") %>'></asp:TextBox>
		</EditItemTemplate>
		<FooterTemplate>
			<asp:TextBox id="txtAddUserName" size="5" runat="server"></asp:TextBox>
		</FooterTemplate>
	</asp:TemplateField>

	<asp:TemplateField HeaderText="Password">
		<ItemTemplate>
			<asp:Label id="lblPassword" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Password") %>'></asp:Label>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:TextBox id="txtEditPassword" size="10" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Password") %>'></asp:TextBox>
		</EditItemTemplate>
		<FooterTemplate>
			<asp:TextBox id="txtAddPassword" size="10" runat="server"></asp:TextBox>
		</FooterTemplate>
	</asp:TemplateField>

	<asp:TemplateField HeaderText="EmailAdddress">
		<ItemTemplate>
			<asp:Label id="lblEmailAdddress" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.EmailAdddress") %>'></asp:Label>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:TextBox id="txtEditEmailAdddress" size="20" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.EmailAdddress") %>'></asp:TextBox>
		</EditItemTemplate>
		<FooterTemplate>
			<asp:TextBox id="txtAddEmailAdddress" size="20" runat="server"></asp:TextBox>
		</FooterTemplate>
	</asp:TemplateField>

	<asp:TemplateField HeaderText="ชื่อ - สกุล">
		<ItemTemplate>
			<asp:Label id="lblFullName" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.FullName") %>'></asp:Label>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:TextBox id="txtEditFullName" size="2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.FullName") %>'></asp:TextBox>
		</EditItemTemplate>
		<FooterTemplate>
			<asp:TextBox id="txtAddFullName" size="2" runat="server"></asp:TextBox>
		</FooterTemplate>
	</asp:TemplateField>

	<asp:TemplateField HeaderText="หน่วยงาน">
		<ItemTemplate>
			<asp:Label id="lblAgencies" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Agencies") %>'></asp:Label>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:TextBox id="txtEditAgencies" size="6" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Agencies") %>'></asp:TextBox>
		</EditItemTemplate>
		<FooterTemplate>
			<asp:TextBox id="txtAddAgencies" size="6" runat="server"></asp:TextBox>
		</FooterTemplate>
	</asp:TemplateField>

    <asp:TemplateField HeaderText="ตำแหน่ง">
		<ItemTemplate>
			<asp:Label id="lblPosition" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Position") %>'></asp:Label>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:TextBox id="txtEditPosition" size="6" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Position") %>'></asp:TextBox>
		</EditItemTemplate>
		<FooterTemplate>
			<asp:TextBox id="txtAddPosition" size="6" runat="server"></asp:TextBox>
		</FooterTemplate>
	</asp:TemplateField>

	<asp:TemplateField HeaderText="ประเภทสมาชิก">
		<ItemTemplate>
			<asp:Label id="lblUserType" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UserType") %>'></asp:Label>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:TextBox id="txtEditUserType" size="6" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UserType") %>'></asp:TextBox>
		</EditItemTemplate>
		<FooterTemplate>
			<asp:TextBox id="txtAddUserType" size="6" runat="server"></asp:TextBox>

			<asp:Button id="btnAdd" runat="server" Text="เพิ่ม" CommandName="Add"></asp:Button>
		</FooterTemplate>
	</asp:TemplateField>

	<asp:CommandField ShowEditButton="True" CancelText="ยกเลิก" DeleteText="ลบ" EditText="แก้ไข" UpdateText="บันทึก" HeaderText="แก้ไข"  />
	<asp:CommandField ShowDeleteButton="True" HeaderText="ลบ" />
	
	</Columns>
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>



Code (C#)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace WebApp
{
    public partial class AdminGridView : System.Web.UI.Page
    {
            SqlConnection objConn;
            SqlCommand objCmd;
            String strSQL;
        protected void Page_Load(object sender, EventArgs e)
        {
            String strConnString;
            strConnString = "Server=localhost;UID=sa;PASSWORD=12345;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
            objConn = new SqlConnection(strConnString);
            objConn.Open();

            if (!Page.IsPostBack)
            {
                BindData();
            }
        }
        protected void BindData()
        {
            String strSQL;
            strSQL = "SELECT * FROM Register";

            SqlDataReader dtReader;
            objCmd = new SqlCommand(strSQL, objConn);
            dtReader = objCmd.ExecuteReader();

            //*** BindData to GridView ***//
            myGridView.DataSource = dtReader;
            myGridView.DataBind();

            dtReader.Close();
            dtReader = null;

        }

        protected void Page_UnLoad()
        {
            objConn.Close();
            objConn = null;
        }

        protected void modEditCommand(object sender, GridViewEditEventArgs e)
        {
            myGridView.EditIndex = e.NewEditIndex;
            myGridView.ShowFooter = false;
            BindData();
        }

        protected void modCancelCommand(object sender, GridViewCancelEditEventArgs e)
        {
            myGridView.EditIndex = -1;
            myGridView.ShowFooter = true;
            BindData();
        }

        protected void modDeleteCommand(object sender, GridViewDeleteEventArgs e)
        {
            strSQL = "DELETE FROM Register WHERE UserName = '" + myGridView.DataKeys[e.RowIndex].Value + "'";
            objCmd = new SqlCommand(strSQL, objConn);
            objCmd.ExecuteNonQuery();

            myGridView.EditIndex = -1;
            BindData();
        }

        protected void modUpdateCommand(object sender, GridViewUpdateEventArgs e)
        {
            //*** UserName ***//
            TextBox txtUserName = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditUserName");
            //*** Password ***//
            TextBox txtPassword = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditPassword");
            //*** Email ***//
            TextBox txtEmailAdddress = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditEmailAdddress");
            //*** FullName ***//
            TextBox txtFullName = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditFullName");
            //*** Agencies ***//
            TextBox txtAgencies = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditAgencies");
            //*** Position ***//
            TextBox txtPosition = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditPosition");
            //*** UserType ***//
            TextBox txtUserType = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditUserType");

            strSQL = "UPDATE Register SET UserName = '" + txtUserName.Text + "' " +
                " ,Password = '" + txtPassword.Text + "' " +
                " ,EmailAdddress = '" + txtEmailAdddress.Text + "' " +
                " ,FullName = '" + txtFullName.Text + "' " +
                " ,Agencies = '" + txtAgencies.Text + "' " +
                " ,Position = '" + txtPosition.Text + "' " +
                " ,UserType = '" + txtUserType.Text + "' " +
                " WHERE UserName = '" + myGridView.DataKeys[e.RowIndex].Value + "'";
            objCmd = new SqlCommand(strSQL, objConn);
            objCmd.ExecuteNonQuery();

            myGridView.EditIndex = -1;
            myGridView.ShowFooter = true;
            BindData();
        }
        protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Add")
            {

                //*** UserName ***//
                TextBox txtUserName = (TextBox)myGridView.FooterRow.FindControl("txtAddUserName");
                //*** Password ***//
                TextBox txtPassword = (TextBox)myGridView.FooterRow.FindControl("txtAddPassword");
                //*** Email ***//
                TextBox txtEmailAdddress = (TextBox)myGridView.FooterRow.FindControl("txtAddEmailAdddress");
                //*** FullName ***//
                TextBox txtFullName = (TextBox)myGridView.FooterRow.FindControl("txtAddFullName");
                //*** Agencies ***//
                TextBox txtAgencies = (TextBox)myGridView.FooterRow.FindControl("txtAddAgencies");
                //*** Position ***//
                TextBox txtPosition = (TextBox)myGridView.FooterRow.FindControl("txtAddPosition");
                //*** UserType ***//
                TextBox txtUserType = (TextBox)myGridView.FooterRow.FindControl("txtAddUserType");

                strSQL = "INSERT INTO Register (UserName,Password,EmailAdddress,FullName,Agencies,Position,UserType) " +
                    " VALUES ('" + txtUserName.Text + "','" + txtPassword.Text + "','" + txtEmailAdddress.Text + "' " +
                    " ,'" + txtFullName.Text + "','" + txtAgencies.Text + "','" + txtPosition.Text + "','" + txtUserType.Text + "') ";
                objCmd = new SqlCommand(strSQL, objConn);
                objCmd.ExecuteNonQuery();

                BindData();
            }
        }
    }
}




Tag : .NET, Ms SQL Server 2005, C#







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2012-12-13 21:11:25 By : Kaen17 View : 1313 Reply : 4
 

 

No. 1



โพสกระทู้ ( 231 )
บทความ ( 0 )



สถานะออฟไลน์


ช่วยหน่อยครับ TT






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-12-13 23:07:52 By : Kaen17
 


 

No. 2

Guest


จะเอาคอลัมไหนเปน dropdownlist ครับ

วิธีการ

Code (VB.NET)
<Columns>
	<asp:TemplateField HeaderText="UserName">
		<EditItemTemplate>
                                                <asp:dropdownlist id="txteditinstructor" backColor=Orange  runat="server" 
                                                                    value='<%# DataBinder.Eval(Container, "DataItem.instructor") %>'>
                                                </asp:dropdownlist>
               </EditItemTemplate>
</Columns>	



วิธีการเรียกใช้

Code (VB.NET)
Dim txteditinstrutor As DropDownList = CType(myGridView.Rows(e.RowIndex).FindControl("txteditinstructor"), DropDownList)

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-12-14 08:38:31 By : yo
 

 

No. 3



โพสกระทู้ ( 231 )
บทความ ( 0 )



สถานะออฟไลน์


จะเอาคอลัม

Agencies
Position
UserType
เป็น C# ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-12-14 09:32:44 By : Kaen17
 


 

No. 4



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ตัวอย่างนี้ครับ

Go to : ASP.NET GridView Control - DropDownList in GridView
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-12-14 09:51:39 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ASP.NET จะแสดงข้อมูลที่เป็น DropDownList บนใน GridView ทำไงครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่