Bind Grid / Postback Problem

Discussion in 'ASP .Net Datagrid Control' started by Steve, Nov 14, 2003.

  1. Steve

    Steve Guest

    Hi

    I have a db with 2 tables that I want to bind to a grid depending on a
    selection in a Dropdownlist
    Also I want to be able to select a row from the gris to fill some
    textboxes.
    The databases are static in that they will not be updated they are just
    for viewing

    I have everything working but not to perfection

    A few issues I am trying to solve
    1) If I select a row from the select button - can I avoid a postback and
    just fill the textboxes with the
    data in the grid row

    2)It seems that I have to have the Grid.Bind() in the Page_Load - I
    thought I would only need to load the
    Datasets once and just re-bind to Grid????

    3)The one problem I do have is that if I hit a CommandButton that
    actually needs to go to the server
    I lose the grid


    Thanks
    Steve


    Dim MyConnection As SqlConnection
    Dim DS As DataSet
    Dim DS2 As DataSet

    Public Class DLLClass
    <DllImport("Payment.dll")> _
    Public Shared Function _
    CalcPaymentCH(ByVal lLOS As Integer, ByVal dCW As Double, ByVal
    lLTP As Integer, ByVal lHTP As Integer, ByVal dALOS As Double, ByVal
    dCHF As Double, ByVal dFACTOR As Double) As Double
    End Function

    End Class


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    MyConnection = New SqlConnection("Data Source=localhost;Initial
    Catalog=Stats;User Id=sa;Password=;")

    If Not (IsPostBack) Then ' Evals true first time browser hits
    the page
    'Put user code to initialize the page here
    'BindGrid() 'Should be here ????

    ddlFormula.Items.Add("")
    ddlFormula.Items.Add("Swiss 2003")
    ddlFormula.Items.Add("Swiss 2004")



    txtCHF.Text = "5000"
    txtFactor.Text = "0.7"
    txtHTPF.Text = "2.43"
    txtLTPF.Text = "2.0"

    txtLTPF.Visible = False
    txtHTPF.Visible = False
    lblLTPF.Visible = False
    lblHTPF.Visible = False

    End If

    BindGrid()

    End Sub



    Private Sub ddlFormula_SelectedIndexChanged(ByVal sender As
    System.Object, ByVal e As System.EventArgs) Handles
    ddlFormula.SelectedIndexChanged

    If (ddlFormula.SelectedValue = "Swiss 2004") Then
    txtLTPF.Visible = True
    txtHTPF.Visible = True
    lblLTPF.Visible = True
    lblHTPF.Visible = True
    Datagrid1.DataSource = DS2.Tables("CHAP2004").DefaultView
    Datagrid1.DataBind()
    Else
    txtLTPF.Visible = False
    txtHTPF.Visible = False
    lblLTPF.Visible = False
    lblHTPF.Visible = False
    Datagrid1.DataSource = DS.Tables("CHAP2003").DefaultView
    Datagrid1.DataBind()
    End If

    End Sub

    Private Sub BindGrid()

    Dim MyCommand As SqlDataAdapter

    MyCommand = New SqlDataAdapter("SELECT * FROM CHAP2003",
    MyConnection)
    DS = New DataSet
    MyCommand.Fill(DS, "CHAP2003")

    MyCommand = New SqlDataAdapter("SELECT * FROM CHAP2004",
    MyConnection)
    DS2 = New DataSet
    MyCommand.Fill(DS2, "CHAP2004")

    'Datagrid1.DataSource = DS.Tables("CHAP2003").DefaultView
    'Datagrid1.DataBind()

    End Sub


    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal
    e As System.EventArgs) Handles btnCalculate.Click
    Dim dPayment As Double
    If (ddlFormula.SelectedValue = "Swiss 2003") Then
    dPayment = DLLClass.CalcPaymentCH(20, 1.5, 5, 15, 10.5,
    5000.0, 0.7)
    ElseIf (ddlFormula.SelectedValue = "Swiss 2004") Then

    End If
    txtPayment.Text = Str(dPayment)
    End Sub



    Private Sub Datagrid1_SelectedIndexChanged(ByVal sender As Object,
    ByVal e As System.EventArgs) Handles Datagrid1.SelectedIndexChanged
    txtDRG.Text = Datagrid1.SelectedItem.Cells(1).Text.ToString()
    txtWT.Text = Datagrid1.SelectedItem.Cells(2).Text.ToString()
    txtALOS.Text = Datagrid1.SelectedItem.Cells(3).Text.ToString()
    txtHTP1.Text = Datagrid1.SelectedItem.Cells(4).Text.ToString()
    txtLTP.Text = Datagrid1.SelectedItem.Cells(5).Text.ToString()
    End Sub
    End Class


    <div style="Z-INDEX: 122; LEFT: 205px; OVERFLOW: auto; WIDTH: 250px;
    POSITION: absolute; TOP: 62px; HEIGHT: 185px"><asp:datagrid
    id="Datagrid1" runat="server" EnableViewState="False" Width="200px">
    <AlternatingItemStyle BorderColor="White"
    BackColor="#C0C0FF"></AlternatingItemStyle>
    <Columns>
    <asp:ButtonColumn Text="Select"
    CommandName="Select"></asp:ButtonColumn>
    </Columns>
    </asp:datagrid></div>

    <asp:button id="btnCalculate" style="Z-INDEX: 110; LEFT: 69px; POSITION:
    absolute; TOP: 534px"
    runat="server" Height="23px" Width="166px" Text="Calculate
    Payment"></asp:button>
     
    Steve, Nov 14, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AD
    Replies:
    1
    Views:
    1,064
    Francis Shanahan
    Aug 23, 2003
  2. Steve

    Bind Grid / Postback Problem

    Steve, Nov 14, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,787
    Steve
    Nov 14, 2003
  3. Girish
    Replies:
    4
    Views:
    723
    Patrick.O.Ige
    Jul 19, 2005
  4. mr2_93
    Replies:
    1
    Views:
    3,667
    mr2_93
    Oct 2, 2005
  5. Replies:
    0
    Views:
    614
Loading...

Share This Page