473,385 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Change DataGrid EditControl based On Column Value

Change DataGrid EditControl On Data Value

Hi,

I have a datagrid, and on editing, I want to change the control in the third
colunm based on the value of the first column.

The value in the first column can only be either "Text" or "Image", which is
selcted in a dropdown list.

If the value in the first colum is "Text" I want third column to edited with
a TextBox, but if the value in first column is "Image" I want it to be edited
with a DropDownList.

What I have been trying to do is Dynamically load the edit control into the
third column in the load event for the dropdown in the first column (code
below), but it throws a "Object reference not set to an instance of an
object" on the "phDtls.Controls.Add" line.

May well be going about this the wrong way.

----------------------------------------------------------------------------
Sub ddlCType_OnLoad(ByVal sender As Object, ByVal e As System.EventArgs)

If _editing Then
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim ds As DataSet = getCatDS(_catid)
Dim dt As DataTable = ds.Tables(0)
Dim dr As DataRow
dr = dt.Rows(_editIndex)

If dr("CType") = "T" Then
ddl.Items(0).Selected = True
ElseIf dr("CType") = "I" Then
ddl.Items(1).Selected = True
Dim ddlTmp As New DropDownList
ddlTmp.ID = "ddlImg"
ddlTmp.Items.Add("image1")
ddlTmp.Items.Add("image2")
phDtls.Controls.Add(ddlTmp)
End If
End If

End Sub
------------------------------------------------------------
Feb 28 '06 #1
2 2107
You can handle either the ItemCreated or ItemDataBound event, e.g.

Private Sub datagrid1_ItemDataBound(ByVal sender As Object, ByVal e As _
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
datagrid1.ItemDataBound

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = _
ListItemType.AlternatingItem Then
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
'clear the controls in the 3rd column
e.Item.Cells(2).Controls.Clear()
If drv("Field1").Equals("Text") Then
Dim txt As New TextBox
'add a textbox in the third column
e.Item.Cells(2).Controls.Add(txt)
Else
Dim ddl As New DropDownList
'you can add code to populate the dropdownlist here
'add a textbox in the third column
e.Item.Cells(2).Controls.Add(ddl)
End If
End If

End Sub
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Billy" wrote:
Change DataGrid EditControl On Data Value

Hi,

I have a datagrid, and on editing, I want to change the control in the third
colunm based on the value of the first column.

The value in the first column can only be either "Text" or "Image", which is
selcted in a dropdown list.

If the value in the first colum is "Text" I want third column to edited with
a TextBox, but if the value in first column is "Image" I want it to be edited
with a DropDownList.

What I have been trying to do is Dynamically load the edit control into the
third column in the load event for the dropdown in the first column (code
below), but it throws a "Object reference not set to an instance of an
object" on the "phDtls.Controls.Add" line.

May well be going about this the wrong way.

----------------------------------------------------------------------------
Sub ddlCType_OnLoad(ByVal sender As Object, ByVal e As System.EventArgs)

If _editing Then
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim ds As DataSet = getCatDS(_catid)
Dim dt As DataTable = ds.Tables(0)
Dim dr As DataRow
dr = dt.Rows(_editIndex)

If dr("CType") = "T" Then
ddl.Items(0).Selected = True
ElseIf dr("CType") = "I" Then
ddl.Items(1).Selected = True
Dim ddlTmp As New DropDownList
ddlTmp.ID = "ddlImg"
ddlTmp.Items.Add("image1")
ddlTmp.Items.Add("image2")
phDtls.Controls.Add(ddlTmp)
End If
End If

End Sub
------------------------------------------------------------

Feb 28 '06 #2
Exactly what i was looking for, thanks Phillip.

"Phillip Williams" wrote:
You can handle either the ItemCreated or ItemDataBound event, e.g.

Private Sub datagrid1_ItemDataBound(ByVal sender As Object, ByVal e As _
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
datagrid1.ItemDataBound

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = _
ListItemType.AlternatingItem Then
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
'clear the controls in the 3rd column
e.Item.Cells(2).Controls.Clear()
If drv("Field1").Equals("Text") Then
Dim txt As New TextBox
'add a textbox in the third column
e.Item.Cells(2).Controls.Add(txt)
Else
Dim ddl As New DropDownList
'you can add code to populate the dropdownlist here
'add a textbox in the third column
e.Item.Cells(2).Controls.Add(ddl)
End If
End If

End Sub
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Billy" wrote:
Change DataGrid EditControl On Data Value

Hi,

I have a datagrid, and on editing, I want to change the control in the third
colunm based on the value of the first column.

The value in the first column can only be either "Text" or "Image", which is
selcted in a dropdown list.

If the value in the first colum is "Text" I want third column to edited with
a TextBox, but if the value in first column is "Image" I want it to be edited
with a DropDownList.

What I have been trying to do is Dynamically load the edit control into the
third column in the load event for the dropdown in the first column (code
below), but it throws a "Object reference not set to an instance of an
object" on the "phDtls.Controls.Add" line.

May well be going about this the wrong way.

----------------------------------------------------------------------------
Sub ddlCType_OnLoad(ByVal sender As Object, ByVal e As System.EventArgs)

If _editing Then
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim ds As DataSet = getCatDS(_catid)
Dim dt As DataTable = ds.Tables(0)
Dim dr As DataRow
dr = dt.Rows(_editIndex)

If dr("CType") = "T" Then
ddl.Items(0).Selected = True
ElseIf dr("CType") = "I" Then
ddl.Items(1).Selected = True
Dim ddlTmp As New DropDownList
ddlTmp.ID = "ddlImg"
ddlTmp.Items.Add("image1")
ddlTmp.Items.Add("image2")
phDtls.Controls.Add(ddlTmp)
End If
End If

End Sub
------------------------------------------------------------

Mar 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Faith | last post by:
I found code on www.codeproject.com. Here is a sample it is called DataGridColumnDropDown. I modified it to use the text box when I wanted it to depended on a certain search criteria. Public...
3
by: PeterZ | last post by:
G'day, After doing much searching and pinching bits of ideas from here there and everywhere I came up with a fairly 'clean' solution of including a comboBox into a dataGrid column. You can...
0
by: Amber | last post by:
There are times when you will need to highlight or otherwise modify the contents of a particular DataGrid row-column value based upon the value in the column. In this example we will select the...
2
by: Jim Heavey | last post by:
If I have a datagrid in which a field is bound to a particular field in a datatable which is of a "bool" type. If I want the datagrid to show "Yes" and "No" based upon if the bool field is true or...
7
by: CanoeGuy | last post by:
I have been trying for the last two weeks to display a dynamic DataGrid. The data that I'm pulling from a SQL Server DB will have whole columns that will be either NULL or 0. I want to display...
3
by: Richard | last post by:
I'm trying to change a value of a cell based on another before the grid is displayed. I'm using the ItemDataBound event like this for a simple test: Select Case e.Item.ItemType Case...
2
by: Tor Inge Rislaa | last post by:
How to change row height in a DataGrid I have DataGrid that is filled with data from a table in a DataSet. The content of the cells is text of more than one line (as a note field). What I...
1
by: Mike P | last post by:
Is it possible when populating a datagrid to populate a dropdownlist column based upon the value populated to another row in the datagrid? (i.e. I have a drop down which I want to populate...
0
by: zhuang | last post by:
Hi, Adding combobox to datagrid has been posted many times. I have a datagrid which has multiple combobox columns and normal textbox columns. But how could I change other combo box values at...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.