469,602 Members | 1,838 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

change value of textbox in datagrid

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("tP riority"),
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("tP riority"),
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!

Jan 30 '06 #1
2 12360
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),curren cymanager)

'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

"simon" <me@here.com> wrote in message
news:hk********************************@4ax.com...
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("tP riority"),
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("tP riority"),
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!

Jan 30 '06 #2
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 :)

On Mon, 30 Jan 2006 09:40:36 -0600, "AMDRIT" <am****@hotmail.com>
wrote:
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),curre ncymanager)

'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

"simon" <me@here.com> wrote in message
news:hk********************************@4ax.com.. .
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("tP riority"),
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("tP riority"),
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!


Jan 31 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ronald S. Cook | last post: by
2 posts views Thread by Tor Inge Rislaa | last post: by
1 post views Thread by AliRezaGoogle | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.