Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > datagridview column cel value multiply to textbox value
 

datagridview column cel value multiply to textbox value

hi!

i'm new in VB9

pls help me.

i have a datagridview with 2 column

what i'd like to happen is the 1st column will format to text while the other is currency

and if a user will press a button clear the data in datagridview will clear..

thnx......



roy
VBras  Monday, March 16, 2009 9:09 AM

Hi VBras,

Thank you for your reply!

> the 3rd column of datagrid will display the product of 2 textboxes and the 5th column of datagrid will display the product of 1st column & 3rd column...

Now the DataGridView has 5 DataGridViewTextBoxColumn in it. The following is the sample code to implement the above requirement:

PrivateSubDataGridView1_CellFormatting(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.DataGridViewCellFormattingEventArgs)HandlesDataGridView1.CellFormatting
Dimval1,val2AsInteger
If(e.ColumnIndex=2Ande.RowIndex>=0)Then
IfInteger.TryParse(Me.TextBox1.Text,val1)AndInteger.TryParse(Me.TextBox2.Text,val2)Then
Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value=val1*val2
EndIf
ElseIfe.ColumnIndex=4Then
IfInteger.TryParse(Me.DataGridView1.Rows(e.RowIndex).Cells(0).FormattedValue.ToString(),val1)And_
Integer.TryParse(Me.DataGridView1.Rows(e.RowIndex).Cells(2).FormattedValue.ToString(),val2)Then
Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value=val1*val2
EndIf
EndIf
EndSub

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Linda Liu  Friday, March 20, 2009 6:59 AM

Hi Roy,

> what i'd like to happen is the 1st column will format to text while the other is currency

Youcan handle the CellFormatting event of the DataGridView to do this. For example:

PrivateSubDataGridView1_CellFormatting(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.DataGridViewCellFormattingEventArgs)HandlesDataGridView1.CellFormatting
If(e.ColumnIndex=0)Then
e.Value="sometext"
EndIf
EndSub

> and if a user will press a button clear the data in datagridview will clear..

If the DataGridView is not bound to a data source, you can clear data in the DataGridView by calling the following line of code:

Me.DataGridView1.Rows.Clear()

If the DataGridView is bound to a data source, set the DataSource property of the DataGridView to Nothing.

Hope this helps.
If youhave any question, please feel free to let me know.

Sincerely,
Linda Liu
Linda Liu  Wednesday, March 18, 2009 10:39 AM
ma'am Linda,

thanx for the code but nothing happens:

I try this one :

Me.DataGridView1.Rows.Clear()

and this one with cellformating:


If(e.ColumnIndex=0)Then
e.Value="sometext"
EndIf


Now i add 3 more columns and2 textboxes

the 3rd column of datagrid will display the product of 2 textboxes and the 5th column of datagrid will display the product of 1st column & 3rd column...

help me pls...
i've already browse some furoms but nothing found...
VBras  Friday, March 20, 2009 5:34 AM

Hi VBras,

Thank you for your reply!

> the 3rd column of datagrid will display the product of 2 textboxes and the 5th column of datagrid will display the product of 1st column & 3rd column...

Now the DataGridView has 5 DataGridViewTextBoxColumn in it. The following is the sample code to implement the above requirement:

PrivateSubDataGridView1_CellFormatting(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.DataGridViewCellFormattingEventArgs)HandlesDataGridView1.CellFormatting
Dimval1,val2AsInteger
If(e.ColumnIndex=2Ande.RowIndex>=0)Then
IfInteger.TryParse(Me.TextBox1.Text,val1)AndInteger.TryParse(Me.TextBox2.Text,val2)Then
Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value=val1*val2
EndIf
ElseIfe.ColumnIndex=4Then
IfInteger.TryParse(Me.DataGridView1.Rows(e.RowIndex).Cells(0).FormattedValue.ToString(),val1)And_
Integer.TryParse(Me.DataGridView1.Rows(e.RowIndex).Cells(2).FormattedValue.ToString(),val2)Then
Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value=val1*val2
EndIf
EndIf
EndSub

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Linda Liu  Friday, March 20, 2009 6:59 AM

You can use google to search for other answers

Custom Search

More Threads

• Please help! Need help for passing variable from a combobox
• using XPandercontrol
• workaround for the expiring ticket during idle time
• DIfficulty with Running Server on Local Machine
• TASKVISION DataLayer - How many table can the datalayer support
• Help
• Is it possible to use the PhotoListView outside of this application?
• Length of Turn - Microseconds
• What's going on with Terrarium?
• How to setup connection string