change value of textbox in datagrid

S

simon

hello,
new to vb.net, have a few questions about DataGrid. I have a dataGrid
that is working pulling a dataset back from a stored proc and binding
to the datagrid for display
the datagrid's first column is a textbox(TemplateColumn), the other 3
columns are just display(BoundColumn).

(1) if the value of the textbox is 0, then i'd like to change it null,
so the box is empty

i tried this...

For Each PickGridItem In draftPickGrid.Items
If Convert.ToInt16(CType(PickGridItem.FindControl("tPriority"),
TextBox).Text) = 0 Then
CType(PickGridItem.FindControl("tPriority"), TextBox).Text = ""
End If
Next

this or variation of it didn't work. help?


(2) after the user click the button, I have a function that kicks off
that i wanted to loop the dataset, get a few values (one if which is
the textbox), and then call a proc to update the data

i tried this....

For Each DemoGridItem In draftPickGrid.Items

EventId = Convert.ToInt16(DemoGridItem.Cells(2).Text)
Priority =
Convert.ToInt16(CType(DemoGridItem.FindControl("tPriority"),
TextBox).Text)
DraftPicks.UpdateDraftPick(groupID, EventId, userID, Priority)
Next

I was getting a "Object reference not set to an instance of an object"
on Priority

also tried these lines to capture the value of the textbox

Dim tb As TextBox
tb = DemoGridItem.FindControl("tPriority")
Priority = Convert.ToInt16(tb.Text)

that didn't work either, same error

i'm using visual studio 2003 and put a break point on the function
that is called to loop and call the update proc, i have these
declaration lines at the top of the function

Dim EventId As Integer
Dim Priority As Integer
Dim DemoGridItem As DataGridItem
Dim DraftPicks As New DraftPicksDB


it will skip over the first three Dim lines, Dim the 4th and then move
to the for loop. in the loop when i hit the priority (textbox)
processing line the error is then thrown.

any help/advice would be appreciated - sample code is always welcome
:)
thanks in advance!
 
A

AMDRIT

I would think it is better to ask the data container for zero length strings
and set the value to null from there.

my perferred way (barring stored procedures.)

for each dr as datarow in ctype(dbgdata.datasource, datatable)
if not dr.isnull("FieldToCheck") andalso ctype(dr("FieldToCheck"),
string).length=0 then
dr.beginedit
dr("FieldToCheck") = system.dbnull.value
dr.endedit
end if
next

you can become even more specific by using

for each dr as datarow in ctype(dbgdata.datasource,
datatable).getchanges(Added or Modified)

Another thing you can try is to monitor when the selected row of a datagrid
changes.

private m_CurrentRowIndex as integer
privatee m_CurrencyManager as CurrencyManager

private sub form_load
m_CurrencyManager =
ctype(me.bindingcontext(dbgdata.datasource),currencymanager)

'Optionally issue
m_CurrencyManager.Refresh
end sub

private sub m_CurrencyManager_PositionChanged(Sender, e) handles
m_CurrencyManager.PositionChanged
'Todo: add logic if you allow column sorting....

if me.dbgDatga.CurrentrowIndex <> m_currentrowindex then

dim dr as datarow= ctype(dbgdata.datasource,
datatable).rows(m_currentrowindex)

if not dr.isnull("FieldToCheck") andalso ctype(dr("FieldToCheck"),
string).length=0 then
dr.beginedit
dr("FieldToCheck") = system.dbnull.value
dr.endedit
end if

dbgDatga.CurrentrowIndex = m_currentrowindex

'Optionally issue
m_CurrencyManager.Refresh

end if

end sub
 
S

simon

thank you for the reply.
i have figured out what i was looking to do (may not be the best way
so please feel free to comment)

for setting the value of a textbox to null if it contain a "0" after
loading/binding it (for display purposes not storage in the db),
i did....

Private Sub SetPriorityZeroToNull()

Dim DemoGridItem As DataGridItem

For Each DemoGridItem In draftPickGrid.Items
If CType(DemoGridItem.FindControl("tPriority"),
TextBox).Text = "0" Then
CType(DemoGridItem.FindControl("tPriority"),
TextBox).Text = ""
End If
Next

End Sub


the other problems i was having was due to a typo in my code when
naming the ID of the textbox in the grid. good grief :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top