计算员工人数并在gridview中以页脚显示 [英] Count the number of employee and display it in footer in gridview

查看:102
本文介绍了计算员工人数并在gridview中以页脚显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指定的参数超出了有效值的范围。

参数名称:

当我调试代码时,这个错误会显示给我...
$这行的b $ b

 115 :GridView1.FooterRow.Cells( 5 )。文字=计数





我尝试过:



Sub load1()

Dim con As New SqlConnection

Dim cmd As New SqlCommand(Finance )

Dim Adapter As New SqlDataAdapter

cmd.CommandType = CommandType.StoredProcedure

Dim ds As New DataSet

Dim dt As New DataTable

'con.ConnectionString =Server = .; Database = log; User Id = sa; Password = 123;

Dim constring = ConfigurationManager。 ConnectionStrings(techConnectionString)。ConnectionString()

con.ConnectionString = constring

cmd.Connection = con

Adapter.SelectCommand = cmd

'ds.Tables.Add(dt)

'cmd.Parameters.Add( @Id,SqlDbType.Int)

'cmd.Parameters(@ Id)。Value = txtID.Text

cmd.Parameters.Add(@ m ,SqlDbType.Int)

cmd.Parameters(@ m)。值= 3

con.Open()

适配器。填充(ds,tabel1)

con.Close()

会话(LoadData)= ds.Tables(tabel1)

GridView1.DataSource = ds.Tables(tabel1)

GridView1.DataBind()

'DirectCast(GridView1.FooterRow.Cells(3).FindControl( Label10),Label).Text = dt.Compute(sum(Salary),).ToString()

'GridView1.FooterRow.Cells(0).Text =Total = & GridView1.Rows.Count.ToString()

'Dim sum As Integer = Convert.ToInt32(dt.Compute(SUM(Salary),String.Empty))

'ds.Tables.Add(dt)

dt = ds.Tables(0)

点心作为整数

对于i = 0 to dt.Rows.Count - 1

'dt = ds.Tables(tabel1)

sum = sum + dt.Rows(i)(Salary)

下一页

GridView1.FooterRow.Cells(3).Text = sum



Dim count As Integer = ds.Tables(0).Rows.Count



GridView1.FooterRow.Cells(5).Text = count

End Sub

解决方案

首先查看存储过程财务及其选择的内容。

At猜测(正如理查德所说)它没有返回足够的列。

我强烈建议您使用列的名称而不是使用魔术数字来访问列。相反,SP返回,就像你为薪水所做的那样:

 sum = sum + dt.Rows(i)(  薪水



也许你想要:

< pre lang =C#> GridView1.FooterRow.Cells( NameOfCountColumn)。text = count



但是,不应该在那里设置FooterRow - 它必须等到它被渲染:

GridView.FooterRow属性(System.Web.UI.WebControls) [ ^ ]

注意
仅当GridView控件在RowCreated中创建页脚行后,FooterRow属性才可用事件。



参见例子他们给出了链接。


Specified argument was out of the range of valid values.
Parameter name:
this error display to me when i debug code ...
for this line

Line 115:        GridView1.FooterRow.Cells(5).Text = count



What I have tried:

Sub load1()
Dim con As New SqlConnection
Dim cmd As New SqlCommand("Finance")
Dim Adapter As New SqlDataAdapter
cmd.CommandType = CommandType.StoredProcedure
Dim ds As New DataSet
Dim dt As New DataTable
'con.ConnectionString = "Server=.;Database=log;User Id=sa; Password=123;"
Dim constring = ConfigurationManager.ConnectionStrings("techConnectionString").ConnectionString()
con.ConnectionString = constring
cmd.Connection = con
Adapter.SelectCommand = cmd
'ds.Tables.Add(dt)
'cmd.Parameters.Add("@Id", SqlDbType.Int)
'cmd.Parameters("@Id").Value = txtID.Text
cmd.Parameters.Add("@m", SqlDbType.Int)
cmd.Parameters("@m").Value = 3
con.Open()
Adapter.Fill(ds, "tabel1")
con.Close()
Session("LoadData") = ds.Tables("tabel1")
GridView1.DataSource = ds.Tables("tabel1")
GridView1.DataBind()
'DirectCast(GridView1.FooterRow.Cells(3).FindControl("Label10"), Label).Text = dt.Compute("sum(Salary)", "").ToString()
'GridView1.FooterRow.Cells(0).Text = "Total = " & GridView1.Rows.Count.ToString()
'Dim sum As Integer=Convert.ToInt32(dt.Compute("SUM(Salary)", String.Empty))
'ds.Tables.Add(dt)
dt = ds.Tables(0)
Dim sum As Integer
For i = 0 To dt.Rows.Count - 1
'dt = ds.Tables("tabel1")
sum = sum + dt.Rows(i)("Salary")
Next
GridView1.FooterRow.Cells(3).Text = sum

Dim count As Integer = ds.Tables(0).Rows.Count

GridView1.FooterRow.Cells(5).Text = count
End Sub

解决方案

Start by looking at your stored procedure Finance and what it SELECTs.
At a guess (as Richard says) it doesn't return enough columns.
I would strongly suggest that instead of using "magic numbers" to access your columns, you use the names of the columns that the SP returns instead, just as you do for Salary:

sum = sum + dt.Rows(i)("Salary")


Perhaps you want:

GridView1.FooterRow.Cells("NameOfCountColumn").Text = count


However, the FooterRow should not be set there - it has to wait until it's been rendered:
GridView.FooterRow Property (System.Web.UI.WebControls)[^]

Note
The FooterRow property is available only after the GridView control creates the footer row in the RowCreated event.


See the example they give in the link.


这篇关于计算员工人数并在gridview中以页脚显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆