yes.....here is my code which i tried from this site......
Private Sub SetInitialRow()
Dim dt As New DataTable()
Dim dr As DataRow = Nothing
'Define the Columns
dt.Columns.Add(New DataColumn("RowNumber", GetType(String)))
dt.Columns.Add(New DataColumn("Column1", GetType(String)))
dt.Columns.Add(New DataColumn("Column2", GetType(String)))
dt.Columns.Add(New DataColumn("Column3", GetType(String)))
dt.Columns.Add(New DataColumn("Column4", GetType(String)))
dt.Columns.Add(New DataColumn("Column5", GetType(String)))
'Add a Dummy Data on Initial Load
dr = dt.NewRow()
dr("RowNumber") = 1
dt.Rows.Add(dr)
'Store the DataTable in ViewState
ViewState("CurrentTable") = dt
'Bind the DataTable to the Grid
Gridview1.DataSource = dt
Gridview1.DataBind()
'Extract and Fill the DropDownList with Data
Dim ddl1 As DropDownList = DirectCast(Gridview1.Rows(0).Cells(1).FindControl("ddlProj1"), DropDownList)
Dim ddl2 As DropDownList = DirectCast(Gridview1.Rows(0).Cells(2).FindControl("ddlTask1"), DropDownList)
Dim ddl3 As DropDownList = DirectCast(Gridview1.Rows(0).Cells(3).FindControl("ddlSTask1"), DropDownList)
Dim txt1 As TextBox = DirectCast(Gridview1.Rows(0).Cells(4).FindControl("TxtHours"), TextBox)
Dim txt2 As TextBox = DirectCast(Gridview1.Rows(0).Cells(5).FindControl("TxtDesc_Comp"), TextBox)
End Sub
Private Sub AddNewRowToGrid()
If Page.IsPostBack Then
If ViewState("CurrentTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
drCurrentRow = dtCurrentTable.NewRow()
drCurrentRow("RowNumber") = dtCurrentTable.Rows.Count + 1
'add new row to DataTable
dtCurrentTable.Rows.Add(drCurrentRow)
'Store the current data to ViewState
ViewState("CurrentTable") = dtCurrentTable
For i As Integer = 0 To dtCurrentTable.Rows.Count - 2
'extract the DropDownList Selected Items
Dim ddl1 As DropDownList = DirectCast(Gridview1.Rows(i).Cells(1).FindControl("ddlProj1"), DropDownList)
Dim ddl2 As DropDownList = DirectCast(Gridview1.Rows(i).Cells(2).FindControl("ddlTask1"), DropDownList)
Dim ddl3 As DropDownList = DirectCast(Gridview1.Rows(i).Cells(3).FindControl("ddlSTask1"), DropDownList)
Dim txt1 As TextBox = DirectCast(Gridview1.Rows(0).Cells(4).FindControl("TxtHours"), TextBox)
Dim txt2 As TextBox = DirectCast(Gridview1.Rows(0).Cells(5).FindControl("TxtDesc_Comp"), TextBox)
' Update the DataRow with the DDL Selected Items
dtCurrentTable.Rows(i)("Column1") = ddl1.SelectedItem.Text
dtCurrentTable.Rows(i)("Column2") = ddl2.SelectedItem.Text
dtCurrentTable.Rows(i)("Column3") = ddl3.SelectedItem.Text
dtCurrentTable.Rows(i)("Column4") = txt1.Text
dtCurrentTable.Rows(i)("Column5") = txt2.Text
Next
'Rebind the Grid with the current data
Gridview1.DataSource = dtCurrentTable
Gridview1.DataBind()
End If
Else
Response.Write("ViewState is null")
End If
'Set Previous Data on Postbacks
SetPreviousData()
End If
End Sub
Private Sub SetPreviousData()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
If dt.Rows.Count > 0 Then
For i As Integer = 0 To dt.Rows.Count - 1
'Set the Previous Selected Items on Each DropDownList on Postbacks
Dim ddl1 As DropDownList = DirectCast(Gridview1.Rows(rowIndex).Cells(1).FindControl("ddlProj1"), DropDownList)
Dim ddl2 As DropDownList = DirectCast(Gridview1.Rows(rowIndex).Cells(2).FindControl("ddlTask1"), DropDownList)
Dim ddl3 As DropDownList = DirectCast(Gridview1.Rows(rowIndex).Cells(3).FindControl("ddlStask1"), DropDownList)
Dim txt1 As TextBox = DirectCast(Gridview1.Rows(0).Cells(4).FindControl("TxtHours"), TextBox)
Dim txt2 As TextBox = DirectCast(Gridview1.Rows(0).Cells(5).FindControl("TxtDesc_Comp"), TextBox)
If i < dt.Rows.Count - 1 Then
ddl1.ClearSelection()
ddl1.Items.FindByText(dt.Rows(i)("Column1").ToString()).Selected = True
ddl2.ClearSelection()
ddl2.Items.FindByText(dt.Rows(i)("Column2").ToString()).Selected = True
ddl3.ClearSelection()
ddl3.Items.FindByText(dt.Rows(i)("Column3").ToString()).Selected = True
End If
rowIndex += 1
Next
End If
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim userid As String = Session("UId")
If Not Page.IsPostBack Then
SetInitialRow()
SetInitialRow1()
SetInitialRow2()
End If
End Sub
Protected Sub ButtonAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
AddNewRowToGrid()
End Sub
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
Dim userid As String = Session("UId")
End Sub
Protected Sub Gridview1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gridview1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim userId As String = Session("UId")
Dim constr As String = ConfigurationManager.ConnectionStrings("ISSIDBConnectionString").ConnectionString
Dim cnn As New SqlConnection(constr)
cnn.Open()
Dim ddl1 = DirectCast(e.Row.FindControl("ddlProj1"), DropDownList)
Dim cmd As New SqlCommand("select Proj_Id, Proj_Name from Project_Main order by Proj_Id ", cnn)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
cnn.Close()
ddl1.DataSource = ds
ddl1.DataTextField = "Proj_Name"
ddl1.DataValueField = "Proj_ID"
ddl1.DataBind()
ddl1.Items.Insert(0, New ListItem("Select", "0"))
Dim constr1 As String = ConfigurationManager.ConnectionStrings("ISSIDBConnectionString").ConnectionString
Dim cnn1 As New SqlConnection(constr1)
cnn1.Open()
Dim ddl2 = DirectCast(e.Row.FindControl("ddlTask1"), DropDownList)
Dim cmd1 As New SqlCommand("select Task_Id, Task_Name from Project_Tasks order by Task_Id ", cnn1)
Dim da1 As New SqlDataAdapter(cmd1)
Dim ds1 As New DataSet()
da1.Fill(ds1)
cnn1.Close()
ddl2.DataSource = ds1
ddl2.DataTextField = "Task_Name"
ddl2.DataValueField = "Task_ID"
ddl2.DataBind()
ddl2.Items.Insert(0, New ListItem("Select", "0"))
Dim constr2 As String = ConfigurationManager.ConnectionStrings("ISSIDBConnectionString").ConnectionString
Dim cnn2 As New SqlConnection(constr2)
cnn2.Open()
Dim ddl3 = DirectCast(e.Row.FindControl("ddlSTask1"), DropDownList)
'Dim CountryId As Integer = Convert.ToInt32(e.Row.Cells(0).Text)
Dim cmd2 As New SqlCommand("select SubTask_Id, SubTask_Name from Project_SubTasks order by SubTask_Id ", cnn2)
Dim da2 As New SqlDataAdapter(cmd2)
Dim ds2 As New DataSet()
da2.Fill(ds2)
cnn2.Close()
ddl3.DataSource = ds2
ddl3.DataTextField = "SubTask_Name"
ddl3.DataValueField = "SubTask_ID"
ddl3.DataBind()
ddl3.Items.Insert(0, New ListItem("Select", "0"))
End If
End Sub
Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
'Dim ddl1 As DropDownList = TryCast(sender, DropDownList)
'Response.Write(ddl1.SelectedValue)
Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
For i As Integer = 0 To dt.Rows.Count - 1
Dim rowIndex As Integer
'Dim ddl1 As DropDownList = TryCast(sender, DropDownList)
Dim gvrow As GridViewRow = DirectCast(DirectCast(sender, DropDownList).NamingContainer, GridViewRow)
Dim ddl1 As DropDownList = DirectCast(Gridview1.Rows(rowIndex).Cells(1).FindControl("ddlProj1"), DropDownList)
Dim ddl2 As DropDownList = DirectCast(Gridview1.Rows(rowIndex).Cells(2).FindControl("ddlTask1"), DropDownList)
Dim ddl3 As DropDownList = DirectCast(Gridview1.Rows(rowIndex).Cells(3).FindControl("ddlStask1"), DropDownList)
ddl2.Items.Clear()
Dim constr2 As String = ConfigurationManager.ConnectionStrings("ISSIDBConnectionString").ConnectionString
Dim cnn2 As New SqlConnection(constr2)
Dim sqlstr2 As String = "select Task_Id, Task_Name from Project_Tasks where Proj_Id = '" & ddl1.Text & "' order by Task_Id"
cnn2.Open()
Dim selcmd2 As New SqlCommand(sqlstr2, cnn2)
Dim dr2 As SqlDataReader
dr2 = selcmd2.ExecuteReader
If dr2.HasRows Then
ddl2.DataSource = dr2
ddl2.DataTextField = "Task_Name"
ddl2.DataValueField = "Task_Id"
ddl2.DataBind()
dr2.Close()
cnn2.Close()
'ddl2.Items.Insert(0, "")
ddl2.Items.Insert(0, New ListItem("Select", "0"))
End If
Next
End Sub
Protected Sub ddl2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
For i As Integer = 0 To dt.Rows.Count - 1
Dim rowIndex As Integer
Dim ddl1 As DropDownList = DirectCast(Gridview1.Rows(rowIndex).Cells(1).FindControl("ddlProj1"), DropDownList)
Dim ddl2 As DropDownList = DirectCast(Gridview1.Rows(rowIndex).Cells(2).FindControl("ddlTask1"), DropDownList)
Dim ddl3 As DropDownList = DirectCast(Gridview1.Rows(rowIndex).Cells(3).FindControl("ddlStask1"), DropDownList)
ddl3.Items.Clear()
Dim constr3 As String = ConfigurationManager.ConnectionStrings("ISSIDBConnectionString").ConnectionString
Dim cnn3 As New SqlConnection(constr3)
Dim sqlstr3 As String = "select distinct SubTask_Id,SubTask_Name from Project_SubTasks where Task_Id='" & ddl2.Text & "' order by SubTask_Id"
cnn3.Open()
Dim selcmd4 As New SqlCommand(sqlstr3, cnn3)
Dim dr3 As SqlDataReader
dr3 = selcmd4.ExecuteReader
If dr3.HasRows Then
ddl3.DataSource = dr3
ddl3.DataTextField = "SubTask_Name"
ddl3.DataValueField = "SubTask_Id"
ddl3.DataBind()
dr3.Close()
cnn3.Close()
End If
Next
End Sub