PDA

View Full Version : مبتدی: مشکل در چک باکس دیتا گرید ویو



piter11
شنبه 27 دی 1393, 19:40 عصر
سلام من میخوام با دیتا گرید ویو یه ماشین حساب درست کنم که تیک خورد مبالغ جمع زده بشه حالا کدشو ایراد میگیره دو تا عکسم میزارم کسی میدونه مشکل چیه با سپاس

کد:

Dim ID As Integer = Integer.Parse(DataGridView1.CurrentRow.Cells(0).Va lue.ToString())
For Each row As DataGridViewRow In DataGridView1.Rows

If Convert.ToBoolean(row.Cells("check").Value) = True Then

con = New SqlConnection(My.Settings.Conn)
cmd = New SqlCommand()
cmd.Connection = con


cmd.CommandText = "select sum(fi) from tblfi where id='" + ID.ToString + "'"

con.Open()

Dim sReader As SqlDataReader = cmd.ExecuteReader()


If sReader.Read() Then


'اینجا داده هایی مه توس Sreader خوندیم رو توی تکس باکس ها فراخوانی میکنیم

Label3.Text = sReader.GetValue(0).ToString()


Else : Label3.Text = 0

End If
con.Close()
cmd.Dispose()

End If
Next




127669

ارور کد:
127670

بیتا حکمت
شنبه 27 دی 1393, 20:09 عصر
شاید بین اون سلول فاصله هم هست . با تابع Trim فاصله ها رو حذف کنید و به عدد تبدیل کنید ببنید چی میشه

piter11
شنبه 27 دی 1393, 20:35 عصر
کدوم سلول؟

بیتا حکمت
شنبه 27 دی 1393, 20:58 عصر
کدوم سلول؟



Dim ID As Integer = Integer.Parse(DataGridView1.CurrentRow.Cells(0).Va lue.ToString())


منظورم اینکه (DataGridView1.CurrentRow.Cells(0 به مقداری اشاره می کنه که قابل تبدیل شدن به عدد نیست . این مثال رو با سی شارپ ببنید.





MessageBox.Show(int.Parse("11").ToString()); // این خط بدون مشکل تبدیل میشه
MessageBox.Show(int.Parse("11dsd ").ToString()); // اما این خط ، خطا می ده



حدسم اینکه یه همچین مشکلی اتفاق می افته

gilsoft
یک شنبه 28 دی 1393, 07:12 صبح
سلام دوست عزیز

از کد زیر استفاده کن ببین مشکل برطرف میشه:

Dim ID As Integer = Cint(DataGridView1.CurrentRow.Cells(0).Value)

موفق باشید

piter11
یک شنبه 28 دی 1393, 11:09 صبح
ممنون من الان کد رو به این شکل تغییر دادم تیک میزنم رو دکمه محاسبه کلیک میکنم هیچ اتفاقی نمی افته آخه این کد چه مشکلی داره!

Dim ID As Integer = CInt(DataGridView1.CurrentRow.Cells(0).Value)
For Each row As DataGridViewRow In DataGridView1.Rows

If Convert.ToBoolean(row.Cells("check").Value) = True Then

con = New SqlConnection(My.Settings.Conn)
cmd = New SqlCommand()
cmd.Connection = con
cmd.CommandText = "select sum(fi) from tblfi where id='" + ID + "'"

con.Open()

Dim sReader As SqlDataReader = cmd.ExecuteReader()

If sReader.Read() Then
Label3.Text = sReader.GetValue(0).ToString()
Else : Label3.Text = "0"

End If
con.Close()
cmd.Dispose()

End If
Next

gilsoft
یک شنبه 28 دی 1393, 22:39 عصر
سلام دوست عزیز

اول اینکه: بجای ()Convert.ToBoolean از تابع ()CBool استفاده کن ....


موفق باشید ....