Showing posts with label DropDownList. Show all posts
Showing posts with label DropDownList. Show all posts

How to bind Dropdownlist in Gridview Rowdatabound Using Asp.Net C#

Bind Dropdownlist in Gridview Rowdatabound

Display Some Details in Gridview but we need list of values show to the user on gridview we bind the inside of gridview Dropdownlist using RoeDataBound event method Asp.Net C#.

DEMO



                                   Download


HTML CODING


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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    <table>
        <tr><td>&nbsp;</td><td>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing">
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:TemplateField HeaderText="User Id">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("UserId") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="User Name">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <HeaderStyle ForeColor="#CC3300" />
            </asp:GridView>
            </td></tr>
    </table>
    </div>
    </form>
</body>
</html>



C# CODING


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

public partial class Mysql_StoredProcedure : System.Web.UI.Page
{
    protected void GridBind()
    {
        MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbc"].ToString());
        con.Open();
        MySqlCommand cmd = new MySqlCommand("select * from userinformation", con);

        MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        con.Close();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        GridBind();

    }
   
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // GridViewRow gr = (GridViewRow)FindControl("GridView1");
            DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
            MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbc"].ToString());
            con.Open();
            MySqlCommand cmd1 = new MySqlCommand("select UserName from userinformation", con);
            MySqlDataReader rd1 = cmd1.ExecuteReader();
            ddl.Items.Clear();
            ddl.Items.Add("Select Username");
            while (rd1.Read())
            {
                ddl.Items.Add(rd1[0].ToString());
            }
            con.Close();

            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {

                // GridViewRow gr = (GridViewRow)FindControl("GridView1");
                DropDownList ddll = (DropDownList)e.Row.FindControl("DropDownList1");
                MySqlConnection con1 = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbc"].ToString());
                con1.Open();
                MySqlCommand cmd = new MySqlCommand("select UserName from userinformation", con1);
                MySqlDataReader rd = cmd.ExecuteReader();
                ddl.Items.Clear();
                ddl.Items.Add("Select Username");
                while (rd.Read())
                {
                    ddll.Items.Add(rd[0].ToString());
                }
                con1.Close();

            }
        }
    }
  
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridBind();
    }

}


Add New web form - add Gridview - Edit Column - add - two Template fields 





Bind the User id - Gridview - Edit Template field 





Add - Dropdownlist to Gridview Item Template field




Next - shown below like gridview  - add gridview event - Rowdatabound



Below code for Gridview Itemtemplate Bind Coding





Next - Add Gridview - Commandfield - Edit Field 





Next - Add EditItemTemplate - Bind the Dropdownlist





Next - Shown below like  Gridview - Gridview event - add RowEditing




Below code for Gridview Edit template Bind Coding







Simple Insert,Update,Delete Queries and Bind To Gridview,Dropdownlist Using VB.NET

Simple Insert,Update,Delete Queries and Bind To Gridview,Dropdownlist


Create Simple Insert,Update,Delete,Select Query and Bind To Gridview,Dropdownlist Using in VB.Net 

                                DEMO



                             Download

                         HTML Coding


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Simple Insert,Update,Delete Queries and Bind To Gridview,Dropdownlist Using VB.NET</title>   
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">         
        <table> 
            <tr><td>
        <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
                </td><td>
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td><td></td></tr>
            <tr><td>
        <asp:Label ID="Label2" runat="server" Text="Number"></asp:Label>
                </td><td><asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
                </td><td></td></tr>
            <tr><td>Image</td><td>
        <asp:FileUpload ID="FileUpload1" runat="server" />
                </td><td></td></tr>
            <tr><td class="auto-style1"></td><td class="auto-style1">
        <asp:Button ID="Button1" runat="server" Text="Register" />
<asp:Button ID="Button2" runat="server" Text="Update" />
<asp:Button ID="Button3" runat="server" Text="Delete" />
<asp:Button ID="Button4" runat="server" Text="Select" />
   
                </td><td class="auto-style1"></td></tr>
            <tr><td>&nbsp;Username </td><td>
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
   
                </td><td></td></tr>
            <tr><td></td><td>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
          <asp:ButtonField DataTextField="name" HeaderText="User Name" />
                <asp:ButtonField DataTextField="number" HeaderText="Number" Text="Button" />
                <asp:ImageField ControlStyle-Height="200" DataImageUrlField="imgpath" HeaderText="Image">
<ControlStyle Height="200px"></ControlStyle>
                </asp:ImageField>
            </Columns>
        </asp:GridView>
                </td></tr>
        </table> 

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbcon %>" SelectCommand="SELECT * FROM [reg]"></asp:SqlDataSource>
       
</div>
    </form>
</body>

</html>

                             
                           C# Coding



Imports System.Data
Imports System.Data.SqlClient


Partial Class _Default
    Inherits System.Web.UI.Page

    Dim con As SqlConnection = New SqlConnection()
    ' Database Connection String
    Public Sub dbconn()
        con = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("dbcon").ConnectionString)
        con.Open()
    End Sub
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        BindDropDown()
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Insert Query with Image Upload
        dbconn()
        Dim imgname As String
        imgname = FileUpload1.FileName
        FileUpload1.SaveAs(Server.MapPath("file/" + imgname))

        Dim cmd As SqlCommand = New SqlCommand("insert into reg values('" + txtName.Text + "','" + txtNumber.Text + "','" + imgname + "','" + "file/" + imgname + "')", con)
        cmd.ExecuteNonQuery()
        Response.Write("<script>alert('Data Registered')</script>")
    End Sub

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'Update query

        dbconn()
        Dim cmd As SqlCommand = New SqlCommand("update reg set number= '" + txtNumber.Text + "' where name='" + txtName.Text + "'", con)
        cmd.ExecuteNonQuery()
        Response.Write("<script>alert('Data Updated')</script>")

    End Sub

    Protected Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        ' Delete Query
        dbconn()
        Dim cmd As SqlCommand = New SqlCommand("delete from reg where name='" + txtName.Text + "'", con)
        cmd.ExecuteNonQuery()
        Response.Write("<script>alert('Data Deleted')</script>")
    End Sub

    Protected Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        ' reg table Bind To GridView
        dbconn()
        Dim cmd As SqlCommand = New SqlCommand("select * from reg", con)
        Dim adp As SqlDataAdapter = New SqlDataAdapter(cmd)
        Dim dt As DataTable = New DataTable()
        adp.Fill(dt)
        Dim rd As SqlDataReader = cmd.ExecuteReader()
        If (rd.Read()) Then
            GridView1.DataSource = dt
            GridView1.DataBind()
        Else
            GridView1.DataSource = ""
            GridView1.DataBind()
            Response.Write("<script>alert('No Records')</script>")
        End If

    End Sub
    Protected Sub BindDropDown()
        ' Name bind To Dropdownlist
        dbconn()
        Dim cmd As SqlCommand = New SqlCommand("select name from reg", con)
        Dim rd As SqlDataReader = cmd.ExecuteReader()
        While (rd.Read())
            DropDownList1.Items.Add(rd(0).ToString())
        End While

    End Sub

    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
    End Sub
End Class


Create New Webform - Select Vidual Basic Template 




Add New Form - Visual Basic Web Form 




Select  Textbox,Gridview,Dropdownlist From Toolbox & Bind GridView  




Gridview - Edit Column - Add Imagefield,Boundfield - Datafield Insert Databse column




Create Namespaces For Vb.Net  & Database Connections (dbconn())





Next - Add Insert ,Update,Delete,Select Query in VB.NET