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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional Formatting of Datagrid

Status
Not open for further replies.

JPimp

Technical User
Mar 27, 2007
79
US
I keep getting this error message when I try this code, and I have never done this before (conditional formatting), so I am not sure if that is the issue or something else?

System.InvalidCastException: Unable to cast object of type 'System.EventArgs' to type 'System.Web.UI.WebControls.DataGridItemEventArgs'

This is the code:

Code:
 Dim conMetrics As OleDbConnection = New OleDbConnection(connectString)

        conMetrics.Open()

        Dim strLot
        Dim strPID

        strLot = LotSelect.SelectedItem.Value
        strPID = ProductSelect.SelectedItem.Value

        Dim cmdSelect As OleDbCommand

        If strLot = "Select Lot Number" Then

            cmdSelect = New OleDbCommand("Select * From Metrology Where [PART No_:] = '" & strPID & "' order by [Date_Time] desc", conMetrics)

        ElseIf strPID = "Select Product ID" Then

            cmdSelect = New OleDbCommand("Select * From Metrology Where [Lot No_:] = '" & strLot & "' order by [Date_Time] desc", conMetrics)

        Else

            cmdSelect = New OleDbCommand("Select * From Metrology Where [Lot No_:] = '" & strLot & "' AND [PART No_:] = '" & strPID & "' order by [Date_Time] desc", conMetrics)

        End If


        Dim DataReader As OleDbDataReader

        DataGrid1.DataSource = cmdSelect.ExecuteReader
        DataGrid1.DataBind()

        Dim Sub1, Sub2 As String
        Dim myInteger As Integer

        If (e.Item.ItemType = ListItemType.Item Or _
          e.Item.ItemType = ListItemType.AlternatingItem) Then
            If (e.Item.Cells(3).Text) < "6" Then _
            e.Item.Cells(3).BackColor = System.Drawing.Color.FromName("#ffccff")
            If e.Item.DataItem("Length Top XD") > "5.5" Then _
            e.Item.Cells(3).BackColor = System.Drawing.Color.DeepPink
        End If



        conMetrics.Close()




    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If Page.IsPostBack Then

            Datagrid1_ItemDataBound(Me, New System.EventArgs)


        End If
    End Sub


Kai-What?
 
i think some of your code is missing. and this is just wrong
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   If Page.IsPostBack Then
      Datagrid1_ItemDataBound(Me, New System.EventArgs)
   End If
End Sub
your code should look something like this
Code:
public class mypage : Page
{
   protected override void OnLoad(EventArgs e)
   {
      if (!IsPostback) LoadGrid();
   }

   protected void Grid_ItemDataBound(object sender, DataGridItemEventArgs e)
   {
      if(e.Item.ItemIndex > -1)
      {
         //format row
      }
   }

   protected void Button_Click(object sender, EventArgs e)
   {
      LoadGrid();
   }

   private void LoadGrid()
   {
      MyGrid.DataSource = FetchData();
      myGrid.DataBind();
   }

   private IEnumerable FetchData()
   {
      using(IDbConnection cnn = new OleDbConnection(...))
      {
         IDbCommand cmd = cnn.CreateCommand();
         cmd.Text = "select * from foo where id = @id";
         IParameter parm = cmd.CreateParameter("id");
         param.Value = 1;
         cmd.Parameter.Add(param);

         return new DataTable(cmd.ExecuteReader());
      }
   }
}
you will need to tweak the cmd.Text and cmd.Parameters to make it work in your case, but that should do it.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks jmeckley, I am not a programmer by trade, so I use what has worked in the past, hence the sloppiness, I will try to modify what you suggested and turn it in to VB.NET code.

Thanks!

Kai-What?
 
vb to c# converter
Code:
Public Class mypage 
    Inherits Page 
    Protected Overloads Overrides Sub OnLoad(ByVal e As EventArgs) 
        If Not IsPostback Then 
            LoadGrid() 
        End If 
    End Sub 
    
    Protected Sub Grid_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) 
                'format row 
        If e.Item.ItemIndex > -1 Then 
        End If 
    End Sub 
    
    Protected Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs) 
        LoadGrid() 
    End Sub 
    
    Private Sub LoadGrid() 
        MyGrid.DataSource = FetchData() 
        myGrid.DataBind() 
    End Sub 
    
    Private Function FetchData() As IEnumerable 
        Using cnn As IDbConnection = New OleDbConnection("") 
            Dim cmd As IDbCommand = cnn.CreateCommand() 
            cmd.Text = "select * from foo where id = @id" 
            Dim parm As IParameter = cmd.CreateParameter("id") 
            param.Value = 1 
            cmd.Parameter.Add(param) 
            
            Return New DataTable(cmd.ExecuteReader()) 
        End Using 
    End Function 
End Class

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top