Hi Anjali,
Please refer below sample for bind the DropDownList inside the GridView with the database record.
HTML
<asp:ScriptManager runat="server" />
<div>
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" AllowedFileTypes="jpg,jpeg" MaximumNumberOfFiles="10"
runat="server" OnUploadComplete="AjaxFileUpload1_UploadComplete" OnClientUploadComplete="ClientUploadComplete" />
</div>
<asp:GridView ID="grdtest" runat="server" OnRowDataBound="grdtest_rowdatabound" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:ImageButton ID="imgRedirect" runat="server" Height="50px" Width="50px" ImageUrl="~/Images/trash.png" OnClick="imgRedirect_Click" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="File Name">
<ItemTemplate>
<asp:Label ID="lblName" Text='<%#Eval("ImageName") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:DropDownList ID="ddlType" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Content">
<ItemTemplate>
<asp:TextBox ID="txtDate" TextMode="Date" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Commit" runat="server" OnClick="OnCommit" Height="33px" Width="80px" />
<script type="text/javascript">
function ClientUploadComplete(sender, args) {
if (sender._filesInQueue[sender._filesInQueue.length - 1]._isUploaded)
window.location.reload();
}
</script>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
grdtest.DataSource = GetData("SELECT ImageName FROM tblFilePath");
grdtest.DataBind();
}
}
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string fileName = Path.GetFileName(e.FileName);
string filePath = "~/Uploads/" + fileName;
AjaxFileUpload1.SaveAs(Server.MapPath(filePath));
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
string sql = "INSERT INTO tblFilePath (ImageName,Path) VALUES(@Name, @Path)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@Path", "Uploads/" + fileName);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
protected void OnCommit(object sender, EventArgs e)
{
for (int i = 0; i < grdtest.Rows.Count; i++)
{
GridViewRow row = grdtest.Rows[i];
Label name = row.FindControl("lblName") as Label;
DropDownList type = row.FindControl("ddlType") as DropDownList;
TextBox date1 = row.FindControl("txtDate") as TextBox;
// Update code.
}
}
protected void grdtest_rowdatabound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlTypes = (e.Row.FindControl("ddlType") as DropDownList);
ddlTypes.DataSource = GetData("SELECT Path FROM tblFilePath");
ddlTypes.DataTextField = "Path";
ddlTypes.DataValueField = "Path";
ddlTypes.DataBind();
ddlTypes.Items.Insert(0, new ListItem("Please select"));
}
}
protected void imgRedirect_Click(object sender, EventArgs e)
{
}
private DataTable GetData(string query)
{
string cs = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
grdtest.DataSource = GetData("SELECT ImageName FROM tblFilePath")
grdtest.DataBind()
End If
End Sub
Protected Sub AjaxFileUpload1_UploadComplete(ByVal sender As Object, ByVal e As AjaxControlToolkit.AjaxFileUploadEventArgs)
Dim fileName As String = Path.GetFileName(e.FileName)
Dim filePath As String = "~/Uploads/" & fileName
AjaxFileUpload1.SaveAs(Server.MapPath(filePath))
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using conn As SqlConnection = New SqlConnection(constr)
Dim sql As String = "INSERT INTO tblFilePath (ImageName,Path) VALUES(@Name, @Path)"
Using cmd As SqlCommand = New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@Name", fileName)
cmd.Parameters.AddWithValue("@Path", "Uploads/" & fileName)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using
End Sub
Protected Sub OnCommit(ByVal sender As Object, ByVal e As EventArgs)
For i As Integer = 0 To grdtest.Rows.Count - 1
Dim row As GridViewRow = grdtest.Rows(i)
Dim name As Label = TryCast(row.FindControl("lblName"), Label)
Dim type As DropDownList = TryCast(row.FindControl("ddlType"), DropDownList)
Dim date1 As TextBox = TryCast(row.FindControl("txtDate"), TextBox)
Next
End Sub
Protected Sub grdtest_rowdatabound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ddlTypes As DropDownList = (TryCast(e.Row.FindControl("ddlType"), DropDownList))
ddlTypes.DataSource = GetData("SELECT Path FROM tblFilePath")
ddlTypes.DataTextField = "Path"
ddlTypes.DataValueField = "Path"
ddlTypes.DataBind()
ddlTypes.Items.Insert(0, New ListItem("Please select"))
End If
End Sub
Protected Sub imgRedirect_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Private Function GetData(ByVal query As String) As DataTable
Dim cs As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(cs)
Using cmd As SqlCommand = New SqlCommand(query, con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function