在数据表中添加行时出现问题 [英] Problem in adding rows in a datatable
问题描述
你好!!!
我有两个字段from_year = 2010和curr_year = 2013.我需要从2010年到2013年的每半年将数据添加到数据表中.我尝试过此操作
Hello!!!
I have two fields from_year =2010 and curr_year =2013. I need to add data in datatable for each half year from 2010 to 2013. I Tried this
On Error Resume Next
Dim countLoop As Int16 = 0
Dim count As Int16 = 0
For i As Int16 = from_year To curr_year
count = 0
countLoop = 0
While countLoop < 2
If count = 0 Then
Dim dr As DataRow = dt_Main1.NewRow()
dr("SemesterFrom") = "01-Jan-" + i.ToString()
dr("SemesterTo") = "30-Jun-" + i.ToString()
dr("LeavesForSemester") = 15
dt_Main1.Rows.Add(dr)
Else
Dim dr As DataRow = ds.Tables("Months").NewRow()
dr("SemesterFrom") = "01-Jul-" + i.ToString()
dr("SemesterTo") = "31-Dec-" + from_year.ToString()
dr("LeavesForSemester") = 16
dt_Main1.Rows.Add(dr)
End If
count += 1
countLoop += 1
End While
Next
它的作用是仅添加一部分而不添加其他部分的数据.
可能是因为已经为for循环的该部分添加了行dr.请告诉我如何改进此代码以添加一半的数据,即每年两次,从1月1日到6月30日,从7月1日到12月31日.
what it''s doing is it adds data for only if part not else part.
May be because the row dr is already added for that part of the for loop. Please tell me how can i improve this code to add data for half half i.e two times a year from 1-jan to 30 -Jun and from 1 July to 31 Dec.
推荐答案
您好,Ujjwal,
我看到两个新的行语句彼此不同
Hi Ujjwal,
As I see two new row statements different from Each other
Dim dr As DataRow = dt_Main1.NewRow()
Dim dr As DataRow = ds.Tables("Months").NewRow()
因此,我认为两个DataTable的结构不匹配,您尝试在
So as I think two DataTable''s struct mismatch and you try to add new rows at
dt_Main1
处添加新行,因为这会在其他部分抛出错误
但您提到
there for its throw an error on else part
but you mentioned
On Error Resume Next
继续执行.
结果,它仅执行If部分,而不执行else部分.
there for it continue execution.
As a result it only perform If part and not the else part.
这篇关于在数据表中添加行时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!