It looks like you are struggling with several issues. I think the code below will address most of your questions, and will put you on the right path to using the code behind. This is in VB.Net.
Personally I'm not a fan of the editable gridview, and rather prefer to offer clients edit icons that open modal dialogs to edit records. It gives you much more control over editing records, and the code is much cleaner. But, if it's your requirement to make an editable gridview, then go for it.
This example shows a standard gridview for clarity. Additional formatting markup has also been removed so you can see the core pieces. Below is the markup.
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.v
b" Inherits="Expert._Default"
%>
http://www.w3.org/TR/xhtm
l1/DTD/xht
ml1-transi
tional.dtd
">
http://www.w3.org/1
999/xhtml"
>
Note, this doesn't have all the markup your example indicated, but it does contain your month and year dropdown lists. Those will be populated by for/each loops in the code behind. The ddlUserType list is there to address your question about displaying a concatenated name and cost in the list. Again, your interface is more complex than this, but this will show you the core functionality that I think you are aiming for.
Here is the code behind. To access the code behind, click on the second icon in Solution Explorer with the caption, "Show All Files", then expand the directory next to your aspx file.
The comments above each method explain a little about what they do. Here are a few highlights:
1. Using for loops, the month and year lists are respectively populated by FillMonthList() and FillYearList().
2. Your User Type dropdown list is populated by FillUserTypeList(). This iteratively populates the list one record at a time so you can concatenate the values in the web code, rather than using the DataSource and DataBind methods.
3. There are a couple of examples of using the Command object to execute stored procedures to retrieve data - GetEvents() and GetUserTypes(). I strongly recommend that you put your data queries into stored procedures, rather than constructing them inline within your web code. It reduces the risk of SQL Injection attacks.
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If Page.IsPostBack = False Then
'Populate these lists only when the page first loads.
Call FillMonthList()
Call FillYearList()
End If
Catch ex As Exception
'Handle exception here.
End Try
End Sub
'''
''' Put months in dropdown list.
''' Private Sub FillMonthList()
For MonthID As Integer = 1 To 12
Dim MonthName As String = Left(DateAndTime.MonthName
(MonthID),
3)
ddlMonth.Items.Add(New ListItem(MonthName, MonthID))
Next
ddlMonth.Items.Insert(0, New ListItem("[Month]", 0))
End Sub
'''
''' Put next 15 years in dropdown list.
''' Private Sub FillYearList()
For i As Integer = Now.Year To Now.Year + 15
ddlYear.Items.Add(New ListItem(i, i))
Next
ddlYear.Items.Insert(0, New ListItem("[Year]", 0))
End Sub
'''
''' Iteratively populate ddlUserType, displaying a concatenated UserType+Cost as text.
''' Private Sub FillUserTypeList(ByVal TitleID As Integer)
Dim dt As DataTable = GetUserTypes(TitleID)
For Each dr As DataRow In dt.Rows()
Dim EventID As Integer = CType(dr.Item("intEventID"
), Integer)
Dim UserType As String = dr.Item("strUserTypeName")
.ToString(
).Trim()
Dim Cost As String = dr.Item("fltUserTypeCost")
.ToString.
Trim()
Dim liText As String = String.Format("{0} ${1}", UserType, Cost)
ddlUserType.Items.Add(New ListItem(EventID, liText))
Next
End Sub
'''
''' Repopulate gvRegistration when a month or year is selected.
''' Called by SelectedIndexChanged events of those dropdown lists.
''' Private Sub LoadRegistrationGrid()
Dim MonthID As Integer = 0
Dim YearID As Integer = 0
Integer.TryParse(ddlMonth.
SelectedVa
lue, MonthID)
Integer.TryParse(ddlYear.S
electedVal
ue, YearID)
Dim dt As DataTable = Nothing
If MonthID > 0 AndAlso YearID > 0 Then
dt = GetEvents(MonthID, YearID)
End If
gvRegistration.DataSource = dt
gvRegistration.DataBind()
End Sub 'LoadRegistrationGrid
'''
''' Retrieve events for the selected month and year from the database.
''' Called by LoadRegistrationGrid().
''' Private Function GetEvents(ByVal MonthID As Integer, ByVal YearID As Integer) As DataTable
Dim dtOut As DataTable = Nothing
Dim ConnString As String = System.Configuration.Confi
gurationMa
nager.AppS
ettings("M
yConnectio
nString")
Using cn As New SqlConnection(ConnString)
Using cmd As New SqlCommand("sp_EventsGet",
cn)
cmd.CommandType = CommandType.StoredProcedur
e
cmd.Parameters.Add(New SqlParameter("@MonthID", SqlDbType.Int)).Value() = MonthID
cmd.Parameters.Add(New SqlParameter("@YearID", SqlDbType.Int)).Value() = YearID
cmd.Connection.Open()
Dim ds As DataSet = Nothing
Using da As New SqlDataAdapter(cmd)
da.Fill(ds)
End Using
dtOut = ds.Tables(0)
cmd.Connection.Close()
End Using
End Using
Return dtOut
End Function 'GetEvents
'''
''' Retrieve user types from the database based on the selected title.
''' Called by FillUserTypeList()
''' Private Function GetUserTypes(ByVal TitleID As Integer) As DataTable
Dim dtOut As DataTable = Nothing
Dim ConnString As String = System.Configuration.Confi
gurationMa
nager.AppS
ettings("M
yConnectio
nString")
Using cn As New SqlConnection(ConnString)
Using cmd As New SqlCommand("sp_UserTypesGe
t", cn)
cmd.CommandType = CommandType.StoredProcedur
e
cmd.Parameters.Add(New SqlParameter("@TitleID", SqlDbType.Int)).Value() = TitleID
cmd.Connection.Open()
Dim ds As DataSet = Nothing
Using da As New SqlDataAdapter(cmd)
da.Fill(ds)
End Using
dtOut = ds.Tables(0)
cmd.Connection.Close()
End Using
End Using
Return dtOut
End Function 'GetUserTypes
'''
''' Event to reload the grid when a new month is selected.
''' Private Sub ddlMonth_SelectedIndexChan
ged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMonth.SelectedIndexChan
ged
Try
Call LoadRegistrationGrid()
Catch ex As Exception
'Handle exception here.
End Try
End Sub
'''
''' Event to reload the grid when a new year is selected.
''' Private Sub ddlYear_SelectedIndexChang
ed(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlYear.SelectedIndexChang
ed
Try
Call LoadRegistrationGrid()
Catch ex As Exception
'Handle exception here.
End Try
End Sub
End Class
Good luck. I hope this helps you out.