Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interesting DataGrid Issue 1

Status
Not open for further replies.

LittlBUGer

Programmer
Apr 26, 2006
81
US
Hello all. I have a very strange and interesting DataGrid issue that I've been pulling my hair out at for 2 days now. I'm updating a web-based timekeeping program (if you want to call it that) for my company. There is a certain interface which allows admins to basically edit any information for any employee. I have a DataGrid in this interface with several templatecolumns and a dropdownlist and literal associated with each as well as a button column. I have it in dropdownlists and literals because the admin can choose to edit whatever they want and then basically do a bulk edit.

Anyway, the template columns and dropdownlists have information such as: Record #, Employee, Date, Hours, Project, Task, Comments, etc. The DataGrid is created only after the admin chooses a date range and either an employee or a project. When the datagrid is created, it can have several entries (lines/rows) with the information I said above depending on the date range.

So, the problem... When the DataGrid is initially created, it looks fine. For example, with the sample data I'm using, I get 3 rows with the correct column information in each of the appropriate dropdownlists. Now, the bulk edit works fine here, but that's not the issue. If I select the Edit button in the button column (to edit a single row), and I update the information and hit my Update button, it updates successfully but I have it force the datagird to disappear. Thus I must click on my Show Data button again to make it reappear and see the changes (if I wanted to). When I do this, this is where the problem comes in. It shows the 3 rows, but in the exact row that I just updated all of the dropdownlists are completely empty when, obviously, they shouldn't be. Though the data that's not in dropdownlists, like the date, hours, and comments are just fine. Now it doesn't matter what I do, that same row will stay empty. Even if I chose a completely different employee and different information, that exact same row (even though the actual data doesn't relate at all to the previous data I updated) is still empty. The only way right now for me to fix this is to completely exit the site and come back in.

I have no idea why it's doing this as everything is set for it to show the data properly and all other rows work fine. It's only that one row, no matter which data is there, that's empty. Has anyone had a similar experience of may have a suggestion? I'd be happy to provide some more details if needed, but I was just trying to give a bried overview for my first post. Thanks! :)

Oh, and BTW, I'm programming in VB.NET and ASP.NET 1.1. :)

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
I assume that you save your changes to your database, but after you click the Update button are you querying the database again and rebinding the data to the grid?
 
The Update button saves the changes to the DB and then the page refreshes (of course) and the datagrid disappears. This forces the user to hit the Show Data button again where the DB query and the rebinding occurs. :)

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
Well, I figured out something else about this issue. This not only happens when I hit the Update and Delete buttons, but also when I hit the Show Data button. Remember this is only if the Edit button (selecting a single row in the datagrid to edit) is pressed. So, I don't think it has anything to do with the Update, Delete, or Show Data buttons at all, but rather the ItemDataBound sub or the BindDataGrid sub, I'm not sure which one. Does this help anyone with their knowledge on this? Thanks for any help that you may give. :)

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
ItemDataBound...

Is this where you are binding each rows data on Initial View?

Like...
Code:
Select Case e.Item.ItemType
   Case ListItemType.Item, ListItemType.AlternatingItem
       'Bind The Data To The DropDownLists        
End Select
 
Basically, yes, for each of the dropdownlists in the template columns for each row. I don't know how to do it any other way so if there's a better way, please enlighten me. Here's my code:

ItemDataBound Sub
Code:
Sub Report_ItemDataBound(s as object, e as DataGridItemEventArgs)

	sqlconn.Open()

'	*******************************
'	Populate Employee DD list in DG
	Dim DS As New DataSet()
	Dim EmpID2 As Integer = 0
	Dim strcommand As String = "select UserName, EmpID from Employee order by UserName asc"
	Dim Empchoice As SqlDataAdapter = New SqlDataAdapter(strcommand, sqlconn)
	Empchoice.Fill(DS, "Employee")
	
	'    Find the drop down control from datagrid items
	Dim emplist As DropDownList = CType(e.Item.FindControl("ddUserName"), DropDownList)
	Dim empValue = CType(e.Item.FindControl("litUserName"), Literal)	
	
	If e.Item.ItemType = ListItemType.AlternatingItem or e.Item.ItemType = ListItemType.Item then		
		'    Set the text and value properties for the dropdown list to appropriate columns in the data source
		emplist.DataTextField = "UserName"
		emplist.DataValueField = "EmpID"
		emplist.DataSource = DS.Tables("Employee")
		emplist.DataBind()
'		emplist.selectedindex = e.item.itemindex
		emplist.SelectedIndex = emplist.Items.IndexOf(emplist.Items.FindByValue(empValue.Text))		
		EmpID2 = emplist.Selecteditem.value
	End If

'	*******************************
'	Populate Project DD list in DG	
	strcommand = "select distinct ProjectID, (convert(varchar, projectid) + ' - ' + projectname) as ProjectName FROM task where ProjectActive = 'Y' order by ProjectName asc"
	Dim Projchoice As SqlDataAdapter = New SqlDataAdapter(strcommand, sqlconn)
	Projchoice.Fill(DS, "Task")

	'    Find the drop down control from datagrid items
	Dim ProjNum As Integer = 0
	Dim projlist As DropDownList = CType(e.Item.FindControl("ddProjectName"), DropDownList)
	Dim projValue = CType(e.Item.FindControl("litProjectName"), Literal)
	
	If e.Item.ItemType = ListItemType.AlternatingItem or e.Item.ItemType = ListItemType.Item then
		'    Set the text and value properties for the dropdown list to appropriate columns in the data source
		projlist.DataTextField = "ProjectName"
		projlist.DataValueField = "ProjectID"
		projlist.DataSource = DS.Tables("Task")
		projlist.DataBind()
		projlist.SelectedIndex = projlist.Items.IndexOf(projlist.Items.FindByValue(projValue.Text))
		ProjNum = projlist.selecteditem.value
	End If	

'	*******************************
'	Populate Phase DD list in DG	
	strcommand = "select distinct (convert(varchar, phaseid) + ' - ' + phasename) as phasename, PhaseID from task where ProjectID='" & ProjNum & "' order by phasename asc"
	Dim Phasechoice As SqlDataAdapter = New SqlDataAdapter(strcommand, sqlconn)
	Phasechoice.Fill(DS, "Task2")

	'    Find the drop down control from datagrid items
	Dim PhaseNum As Integer = 0
	Dim phaselist As DropDownList = CType(e.Item.FindControl("ddPhaseName"), DropDownList)
	Dim phaseValue = CType(e.Item.FindControl("litPhaseName"), Literal)		
	
	If e.Item.ItemType = ListItemType.AlternatingItem or e.Item.ItemType = ListItemType.Item then
		'    Set the text and value properties for the dropdown list to appropriate columns in the data source
		phaselist.DataTextField = "PhaseName"
		phaselist.DataValueField = "PhaseID"
		phaselist.DataSource = DS.Tables("Task2")
		phaselist.DataBind()
		phaselist.SelectedIndex = phaselist.Items.IndexOf(phaselist.Items.FindByValue(phaseValue.Text))
		PhaseNum = phaselist.selecteditem.value
	End If
	
'	*******************************
'	Populate Task DD list in DG	
	strcommand = "select distinct (convert(varchar, taskid) + ' - ' + taskname) as taskname, Taskid from task where PhaseID='" & PhaseNum &"' AND projectID ='"& ProjNum & "' order by taskname asc"
	Dim Taskchoice As SqlDataAdapter = New SqlDataAdapter(strcommand, sqlconn)
	Taskchoice.Fill(DS, "Task3")

	'    Find the drop down control from datagrid items
	Dim tasklist As DropDownList = CType(e.Item.FindControl("ddTaskName"), DropDownList)
	Dim taskValue = CType(e.Item.FindControl("litTaskName"), Literal)		
	
	If e.Item.ItemType = ListItemType.AlternatingItem or e.Item.ItemType = ListItemType.Item then
		'    Set the text and value properties for the dropdown list to appropriate columns in the data source
		tasklist.DataTextField = "TaskName"
		tasklist.DataValueField = "Taskid"
		tasklist.DataSource = DS.Tables("Task3")
		tasklist.DataBind()
		tasklist.SelectedIndex = tasklist.Items.IndexOf(tasklist.Items.FindByValue(taskValue.Text))
	End If
	
'	*******************************
'	Populate Clin DD list in DG	
	strcommand = "select distinct Clin.clin, (convert(varchar, clin.clin) + ' - ' + clindesc) as clindesc from clin JOIN ClinEmp ON Clin.Clin = ClinEmp.Clin JOIN Employee ON Employee.EmpID = ClinEmp.EmpID where ClinEmp.EmpID = '" & EmpID2 & "' order by ClinDesc asc"
	Dim Clinchoice As SqlDataAdapter = New SqlDataAdapter(strcommand, sqlconn)
	Clinchoice.Fill(DS, "Clin1")

	'    Find the drop down control from datagrid items
	Dim clinlist As DropDownList = CType(e.Item.FindControl("ddClinDesc"), DropDownList)
	Dim clinValue = CType(e.Item.FindControl("litClinDesc"), Literal)		
	
	If e.Item.ItemType = ListItemType.AlternatingItem or e.Item.ItemType = ListItemType.Item then
		'    Set the text and value properties for the dropdown list to appropriate columns in the data source
		clinlist.DataTextField = "ClinDesc"
		clinlist.DataValueField = "Clin"
		clinlist.DataSource = DS.Tables("Clin1")
		clinlist.DataBind()
		clinlist.SelectedIndex = clinlist.Items.IndexOf(clinlist.Items.FindByValue(clinValue.Text))
	End If	
	
'	*******************************
'	Populate Complete (Employee Lock) DD list in DG	

	'    Find the drop down control from datagrid items
	Dim completelist As DropDownList = CType(e.Item.FindControl("ddcomplete"), DropDownList)
	Dim completeValue = CType(e.Item.FindControl("litcomplete"), Literal)		
	
	If e.Item.ItemType = ListItemType.AlternatingItem or e.Item.ItemType = ListItemType.Item then
		'    Set the text and value properties for the dropdown list to appropriate columns in the data source
		completelist.SelectedIndex = completelist.Items.IndexOf(completelist.Items.FindByValue(completeValue.Text))
	End If					
	
	sqlconn.Close()
	
	Dim thecomments As TextBox = CType(E.Item.FindControl("txtcomment"), TextBox)
	If e.Item.ItemType = ListItemType.AlternatingItem or e.Item.ItemType = ListItemType.Item then
		thecomments.text = thecomments.text.trim()
	End if

End Sub

Please let me know if this helps with a solution to my issue. Thanks for your help! :)

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
Id adjust some of the code to only execute if its in the item or alt item, even declaring variables...BUT...

change this...
If e.Item.ItemType = ListItemType.AlternatingItem or e.Item.ItemType = ListItemType.Item Then

to

If e.Item.ItemType = ListItemType.AlternatingItem or e.Item.ItemType = ListItemType.Item OR ListItemType.SelectedItem

Whats happening is when you click EDIT, the row becomes the SelectedItem, and when u update, the row is still the selected item, which is not a item type of item nor alternating item.

you still need some testing, because you'll need to watch out for the ListItemType.EditItem too...

try out the OR selectedItem first, then we'll help with the err checking and edit handling.
 
Heres my thots...only 1 If Then needed on the ItemType...opens the sql connection for those rows only that need to (not the header and footer rows)
Code:
    Sub Report_ItemDataBound(ByVal s As Object, ByVal e As DataGridItemEventArgs)
        If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.SelectedItem Then
            sqlconn.Open()
            '    *******************************
            '    Populate Employee DD list in DG
            Dim DS As New DataSet()
            Dim EmpID2 As Integer = 0
            Dim strcommand As String = "select UserName, EmpID from Employee order by UserName asc"
            Dim Empchoice As SqlDataAdapter = New SqlDataAdapter(strcommand, sqlconn)
            Empchoice.Fill(DS, "Employee")

            '    Find the drop down control from datagrid items
            Dim emplist As DropDownList = CType(e.Item.FindControl("ddUserName"), DropDownList)
            Dim empValue = CType(e.Item.FindControl("litUserName"), Literal)

            '    Set the text and value properties for the dropdown list to appropriate columns in the data source
            emplist.DataTextField = "UserName"
            emplist.DataValueField = "EmpID"
            emplist.DataSource = DS.Tables("Employee")
            emplist.DataBind()
            '        emplist.selectedindex = e.item.itemindex
            emplist.SelectedIndex = emplist.Items.IndexOf(emplist.Items.FindByValue(empValue.Text))
            EmpID2 = emplist.SelectedItem.Value

            '    *******************************
            '    Populate Project DD list in DG    
            strcommand = "select distinct ProjectID, (convert(varchar, projectid) + ' - ' + projectname) as ProjectName FROM task where ProjectActive = 'Y' order by ProjectName asc"
            Dim Projchoice As SqlDataAdapter = New SqlDataAdapter(strcommand, sqlconn)
            Projchoice.Fill(DS, "Task")

            '    Find the drop down control from datagrid items
            Dim ProjNum As Integer = 0
            Dim projlist As DropDownList = CType(e.Item.FindControl("ddProjectName"), DropDownList)
            Dim projValue = CType(e.Item.FindControl("litProjectName"), Literal)

            '    Set the text and value properties for the dropdown list to appropriate columns in the data source
            projlist.DataTextField = "ProjectName"
            projlist.DataValueField = "ProjectID"
            projlist.DataSource = DS.Tables("Task")
            projlist.DataBind()
            projlist.SelectedIndex = projlist.Items.IndexOf(projlist.Items.FindByValue(projValue.Text))
            ProjNum = projlist.SelectedItem.Value

            '    *******************************
            '    Populate Phase DD list in DG    
            strcommand = "select distinct (convert(varchar, phaseid) + ' - ' + phasename) as phasename, PhaseID from task where ProjectID='" & ProjNum & "' order by phasename asc"
            Dim Phasechoice As SqlDataAdapter = New SqlDataAdapter(strcommand, sqlconn)
            Phasechoice.Fill(DS, "Task2")

            '    Find the drop down control from datagrid items
            Dim PhaseNum As Integer = 0
            Dim phaselist As DropDownList = CType(e.Item.FindControl("ddPhaseName"), DropDownList)
            Dim phaseValue = CType(e.Item.FindControl("litPhaseName"), Literal)

            '    Set the text and value properties for the dropdown list to appropriate columns in the data source
            phaselist.DataTextField = "PhaseName"
            phaselist.DataValueField = "PhaseID"
            phaselist.DataSource = DS.Tables("Task2")
            phaselist.DataBind()
            phaselist.SelectedIndex = phaselist.Items.IndexOf(phaselist.Items.FindByValue(phaseValue.Text))
            PhaseNum = phaselist.SelectedItem.Value

            '    *******************************
            '    Populate Task DD list in DG    
            strcommand = "select distinct (convert(varchar, taskid) + ' - ' + taskname) as taskname, Taskid from task where PhaseID='" & PhaseNum & "' AND projectID ='" & ProjNum & "' order by taskname asc"
            Dim Taskchoice As SqlDataAdapter = New SqlDataAdapter(strcommand, sqlconn)
            Taskchoice.Fill(DS, "Task3")

            '    Find the drop down control from datagrid items
            Dim tasklist As DropDownList = CType(e.Item.FindControl("ddTaskName"), DropDownList)
            Dim taskValue = CType(e.Item.FindControl("litTaskName"), Literal)

            '    Set the text and value properties for the dropdown list to appropriate columns in the data source
            tasklist.DataTextField = "TaskName"
            tasklist.DataValueField = "Taskid"
            tasklist.DataSource = DS.Tables("Task3")
            tasklist.DataBind()
            tasklist.SelectedIndex = tasklist.Items.IndexOf(tasklist.Items.FindByValue(taskValue.Text))

            '    *******************************
            '    Populate Clin DD list in DG    
            strcommand = "select distinct Clin.clin, (convert(varchar, clin.clin) + ' - ' + clindesc) as clindesc from clin JOIN ClinEmp ON Clin.Clin = ClinEmp.Clin JOIN Employee ON Employee.EmpID = ClinEmp.EmpID where ClinEmp.EmpID = '" & EmpID2 & "' order by ClinDesc asc"
            Dim Clinchoice As SqlDataAdapter = New SqlDataAdapter(strcommand, sqlconn)
            Clinchoice.Fill(DS, "Clin1")

            '    Find the drop down control from datagrid items
            Dim clinlist As DropDownList = CType(e.Item.FindControl("ddClinDesc"), DropDownList)
            Dim clinValue = CType(e.Item.FindControl("litClinDesc"), Literal)

            '    Set the text and value properties for the dropdown list to appropriate columns in the data source
            clinlist.DataTextField = "ClinDesc"
            clinlist.DataValueField = "Clin"
            clinlist.DataSource = DS.Tables("Clin1")
            clinlist.DataBind()
            clinlist.SelectedIndex = clinlist.Items.IndexOf(clinlist.Items.FindByValue(clinValue.Text))

            '    *******************************
            '    Populate Complete (Employee Lock) DD list in DG    

            '    Find the drop down control from datagrid items
            Dim completelist As DropDownList = CType(e.Item.FindControl("ddcomplete"), DropDownList)
            Dim completeValue = CType(e.Item.FindControl("litcomplete"), Literal)

            '    Set the text and value properties for the dropdown list to appropriate columns in the data source
            completelist.SelectedIndex = completelist.Items.IndexOf(completelist.Items.FindByValue(completeValue.Text))

            sqlconn.Close()

            Dim thecomments As TextBox = CType(e.Item.FindControl("txtcomment"), TextBox)
            thecomments.Text = thecomments.Text.Trim()
        End If
    End Sub
 
That seems to have done the trick. Thanks! I don't know why I didn't see or think of that before. As for the optimization of the code, I think I've just been so frustrated lately on just getting the damn thing functioning properly that I haven't paid attention to good code writing ethics, lol. Thank you though. I will do more extensive testing and post back if needed. :D

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top