bobsegrest
asked on
How do you programmatically set a datagrid column to readOnly?
Hello Everyone,
I am working on an ASP.Net web page using VB in a Visual Studio 2003 development environment.
I have a datagrid displayed on my page that dynamically integrates data from two databases.
When a row is selected for editing, I need to programmatically set which columns are (or are not) read-only based on the data already contained in the column.
My project specification does not allowed me to hide any of the columns in my datagrid while editing. What I am looking for is the programmatic equivalent to checking the ReadOnly check box for the column.
Can anyone tell me how to do this?
Bob
I am working on an ASP.Net web page using VB in a Visual Studio 2003 development environment.
I have a datagrid displayed on my page that dynamically integrates data from two databases.
When a row is selected for editing, I need to programmatically set which columns are (or are not) read-only based on the data already contained in the column.
My project specification does not allowed me to hide any of the columns in my datagrid while editing. What I am looking for is the programmatic equivalent to checking the ReadOnly check box for the column.
Can anyone tell me how to do this?
Bob
ASKER
Hi Ben,
When the client clicks the edit link for a row in the datagrid, I need to programmatically decide which columns are to be edited and which are not. By default, all of the columns are editable and thus are displayed as text boxes. If I set a column to ReadOnly in the designer, the column is displayed as a label rather than a textbox. What I am seeking is a way to make this selection dynamically at run time.
Can you provide me with a code example of how I "can programmatically change the column in edit mode in the ItemCreated event of the DataGrid"?
Bob
When the client clicks the edit link for a row in the datagrid, I need to programmatically decide which columns are to be edited and which are not. By default, all of the columns are editable and thus are displayed as text boxes. If I set a column to ReadOnly in the designer, the column is displayed as a label rather than a textbox. What I am seeking is a way to make this selection dynamically at run time.
Can you provide me with a code example of how I "can programmatically change the column in edit mode in the ItemCreated event of the DataGrid"?
Bob
Hi,
What you need to do is to handle this logic in the datagrid editcommand event that fires when the user selects a row and thw way of doing this is as follows:
private void DataGrid1_EditCommand(obje ct source, System.Web.UI.WebControls. DataGridCo mmandEvent Args e)
{ TextBox box = new TextBox();
// By default edit item in the datgrid will be textbox always so you will need
// To cast to a textbox and then based on your calcaulation set it to readonly or whatever
// the index here is the column index of the datagrid
box = (TextBox) e.Item.Cells[0];
if( certain logic that you need to handle)
{
box.ReadOnly = true;
}
}
good luck,
meomar
What you need to do is to handle this logic in the datagrid editcommand event that fires when the user selects a row and thw way of doing this is as follows:
private void DataGrid1_EditCommand(obje
{ TextBox box = new TextBox();
// By default edit item in the datgrid will be textbox always so you will need
// To cast to a textbox and then based on your calcaulation set it to readonly or whatever
// the index here is the column index of the datagrid
box = (TextBox) e.Item.Cells[0];
if( certain logic that you need to handle)
{
box.ReadOnly = true;
}
}
good luck,
meomar
ASKER
Can you show me how to do this in a Visual Basic (VB) environment?
Sub ItemsGrid_Edit(sender As Object, e As DataGridCommandEventArgs)
Dim qtyText As TextBox = CType(e.Item.Cells(3).Cont rols(0), TextBox)
Dim priceText As TextBox = CType(e.Item.Cells(4).Cont rols(0), TextBox)
Dim item As String = e.Item.Cells(3).Text
Dim qty As String = qtyText.Text
if qty = "1"
priceText.ReadOnly= true
endif
end sub
I am sorry I do not write in VB, but it is technicly the same concepts
Dim qtyText As TextBox = CType(e.Item.Cells(3).Cont
Dim priceText As TextBox = CType(e.Item.Cells(4).Cont
Dim item As String = e.Item.Cells(3).Text
Dim qty As String = qtyText.Text
if qty = "1"
priceText.ReadOnly= true
endif
end sub
I am sorry I do not write in VB, but it is technicly the same concepts
ASKER
OK, here is what I tried,
Private Sub dgRescPlans_EditCommand(By Val source As Object, _
ByVal e As System.Web.UI.WebControls. DataGridCo mmandEvent Args) _
Handles dgRescPlans.EditCommand
' Selecting the Edit button causes a postback, so we need to read the dataset we
' saved as a session variable during the Page_Load setup and bind it to the datagrid
dsRescPlans = CType(Session("dsRescPlans "), DataSet)
dgRescPlans.DataBind()
Dim FullOS As TextBox = CType(e.Item.Cells(0).Cont rols(1), TextBox)
FullOS.ReadOnly = True
End Sub
When the 'Dim FullOS' line executed I received a 'Specified cast is not valid. ' error.
For some reason I don't believe the 'e' argument returned with this event includes the selected datarow. It does include an index to the selected datarow, and one normally uses this to set the editItemIndex in the datagrid. For example:
dgRescPlans.EditItemIndex = e.Item.ItemIndex
However at the instant in time when this event is called, the selected line in the datagrid is not yet displayed in 'edit' mode and as such the textbox that you are suggesting I set to read only does not exist.
Clearly one of us is missing something here. Frankly, I'll be happy for it to be me. Please enlighten me...
Bob
Private Sub dgRescPlans_EditCommand(By
ByVal e As System.Web.UI.WebControls.
Handles dgRescPlans.EditCommand
' Selecting the Edit button causes a postback, so we need to read the dataset we
' saved as a session variable during the Page_Load setup and bind it to the datagrid
dsRescPlans = CType(Session("dsRescPlans
dgRescPlans.DataBind()
Dim FullOS As TextBox = CType(e.Item.Cells(0).Cont
FullOS.ReadOnly = True
End Sub
When the 'Dim FullOS' line executed I received a 'Specified cast is not valid. ' error.
For some reason I don't believe the 'e' argument returned with this event includes the selected datarow. It does include an index to the selected datarow, and one normally uses this to set the editItemIndex in the datagrid. For example:
dgRescPlans.EditItemIndex = e.Item.ItemIndex
However at the instant in time when this event is called, the selected line in the datagrid is not yet displayed in 'edit' mode and as such the textbox that you are suggesting I set to read only does not exist.
Clearly one of us is missing something here. Frankly, I'll be happy for it to be me. Please enlighten me...
Bob
ASKER
OK, Here is what I found...
The best place to implement this change is in the dataGrid_ItemDataBound event. This event fires off one time for each row in your grid After the databind and before it is rendered. if e.Item.ItemType = ListItemType.EditItem you are looking at the row selected for editing and you can easily clear (delete) the existing textbox control and add a new control of any type you choose. In my case I used a label control.
If you simply set the original textbox control to read only, the grid will render a dysfunctional textbox.
I hope this helps the next person with this issue....
Please refund my points.
Bob
The best place to implement this change is in the dataGrid_ItemDataBound event. This event fires off one time for each row in your grid After the databind and before it is rendered. if e.Item.ItemType = ListItemType.EditItem you are looking at the row selected for editing and you can easily clear (delete) the existing textbox control and add a new control of any type you choose. In my case I used a label control.
If you simply set the original textbox control to read only, the grid will render a dysfunctional textbox.
I hope this helps the next person with this issue....
Please refund my points.
Bob
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are you putting the data in a TextBox?
Ben.