By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,838 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,838 IT Pros & Developers. It's quick & easy.

Grid Disappears (WebForm)

P: n/a
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>

Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi Steve,
For more information on your questions I'd recommend
reading an article on displaying data in datagrids, there
are a lot of in-depth ones on the web that are easily
accessible.
Briefly:
1) If you want to avoid a postback you'll need to
populate the textboxes using client-side javascript that
fires on the onclick() event of your button. this isn't
easily supported by asp.net server controls, but can be
accomplished.

2) Don't databind() the grid in the Page Load event - in
fact, do the binding inside a {[C#]if (!ispostback)}
condition - this way the viewstate of the datagrid
will 'remember' what was in the grid and re-display it.
If the contents of the grid change you will need to
rebind, remember to obtain the data again first though.

3) Again, you're probably rebinding to the datagrid when
the command-button is hit but there isn't any data to
bind to. Make sure that (a) if you want to rebind, get
the data first; or (b) don't rebind - and the datagrid
will load from its viewstate.

hth
alex
-----Original Message-----
Hi

I have a db with 2 tables that I want to bind to a grid depending on aselection in a Dropdownlist
Also I want to be able to select a row from the gris to fill sometextboxes.
The databases are static in that they will not be updated they are justfor 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 - Ithought 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 thatactually 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, ByVallLTP As Integer, ByVal lHTP As Integer, ByVal dALOS As Double, ByValdCHF As Double, ByVal dFACTOR As Double) As Double
End Function

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

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

If Not (IsPostBack) Then ' Evals true first time browser hitsthe 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 AsSystem.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, ByVale 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:datagridid="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="CalculatePayment"></asp:button>

.

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.