Ask a Question related to ASP.NET Data Grid Control, Design and Development.
-
Steve #1
Bind Grid / Postback Problem
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 Guest
-
merging multiple datasets to bind to a grid
How can I merge 2 datasets into one, then bind with the datagrid, or bind the datagrid to 2 datasets? For instance, I have one stored procedure... -
Bind grid & textbox
I am pulling a query into a cfgrid and if the value pulled in is NULL, when I select that row, and try to edit it using my textbox, it doesn't... -
How to bind to a drop down list in a grid.
I want to display some drop down lists in a grid. Let's say user Joe is from Region 1, and I want to get a list of users and bind to the grid, but... -
How do I dynamically bind a textbox into a grid field in ASP.NET using only the code behind?
I am trying to take dynamically generated datagrid that is bound to a data source and make one of the fields on the grid into an editable textbox... -
How to bind multiple datasources to one grid?
How can I bind my grid to more than one datasource - that is the question. Right now I bind to a DataView table that has a single DataTable as its...



Reply With Quote

