Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Summing a datagrid column??? 1

Status
Not open for further replies.

besiktas

Programmer
Mar 12, 2003
4
TR
Hi,
I've got a dataset and i bound to a datagrid. in this datagrid there is 17 columns. I want to summing a datagrid columns and show the result in footer.
i tried the code i write below but i couldn't get the result:(

int x1 =e.Item.ItemIndex;
int x2 = DataGrid1.Items[x1-1].DataSetIndex;
string name = ds.Tables[0].Columns[x2].ColumnName;
for (int i=0 ;i < e.Item.Cells.Count;i++)
{
if(name!=null && name.CompareTo(&quot;KolonOne&quot;)!=0)
{
if (e.Item.ItemType== ListItemType.Item ||.Item.ItemType== ListItemType.AlternatingItem)
{
Sum[3] += Convert.ToInt64(DataBinder.Eval(e.Item.DataItem,&quot;GVM&quot;));
e.Item.Cells[3].Text+=e.Item.ItemType.ToString();
}
else if (e.Item.ItemType== ListItemType.Footer)
{
e.Item.Cells[3].Text = String.Format(&quot;{0:#,###}&quot;,Sum[3]);
}
}
}

 
I used the following code. I also did this using Page variables and adding the values up but I found this to be faster for me.

HOPE THIS HELPS.

Private Sub dg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemDataBound

Select Case e.Item.ItemType
Case ListItemType.Footer
Dim strConn As String = &quot;server=Server;uid=Login;pwd=Password;Initial Catalog=Database&quot;
Dim cn As SqlConnection = New SqlConnection(strConn)
Dim cmd As New SqlCommand
Dim myReader As SqlDataReader
cmd.Connection = cn
cmd.CommandText = &quot;SELECT SUM(Worked_DC) AS WorkTotal FROM dbo.TimeSheet_T WHERE User_ID = &quot; & cboOtherLogin.SelectedValue & &quot; AND DateOfWork_DT BETWEEN CONVERT(DateTime, '&quot; & Me.calStartingDate.SelectedDate & &quot;', 101) AND CONVERT(DateTime, '&quot; & Me.CalEndingDate.SelectedDate & &quot;', 101)&quot;
Try
cn.Open()
myReader = cmd.ExecuteReader
Do While myReader.Read
If Not IsDBNull(myReader.GetValue(0)) Then
e.Item.Cells(7).Text = CType(myReader.GetValue(0), String)
End If
Loop
Catch ex As Exception
lblError.Text = &quot;A problem was found setting the Total.&quot;
Finally
If Not cn Is Nothing Then
cn.Close()
End If
If Not myReader Is Nothing Then
myReader.Close()
End If
End Try

End Select
End Sub

Hope everyone is having a great day!

Thanks - Jennifer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top