I try to load a table from database and I want to merge data in each column into separate cells. This is following table:
Column A
Column B
Column C
Column D
Cell a1
Cell b1
Cell c1
Cell d1
NULL
Cell b2
Cell c2
NULL
NULL
NULL
Cell c3
NULL
And this is output required
Column A
Column B
Column C
Column D
Cell a1
Cell b1 Cell b2
Cell c1 Cell c2 Cell c3
Cell d1
This is my code to load table in file VB:
Private Sub LOAD_TABLE (ByVal iID As Integer)
Dim cmd As New SqlCommand("LOAD_TB")
Dim s As String = ""
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("#ID", SqlDbType.Int).Value = iID
cmd.Parameters.Add("#T", SqlDbType.Int).Value = DrListT.SelectedValue
cmd.Parameters.Add("#N", SqlDbType.Int).Value = DrListN.SelectedValue
Using con As New SqlConnection(Class_pub.conString)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable
sda.Fill(dt)
GRV_H.DataSource = dt
GRV_H.DataBind()
End Using
End Using
If con.State = ConnectionState.Open Then
con.Close()
con.Dispose()
End If
End Using
End Sub
And the code to display table in file ASPX:
<asp:GridView ID="GRV_H" DataKeyNames="ID" AutoGenerateColumns="False" runat="server"
AllowPaging="False" Width="100%">
<AlternatingRowStyle BackColor="#FCFCFC" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input id="id1" type="text" name="txt1" style="width: 80px; font-family: Arial"
class="textbox_numeric" value='<%# Eval("ColumnA", "{0:#,##0}") %>' onkeyup="this.value=FormatNumber(this.value);"
onclick="return this.focus();" onfocus="this.select();"
autocomplete="Off" runat="server" />
</ItemTemplate>
<HeaderTemplate>
Column A</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="60px" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<input id="id2" type="text" name="txt2" style="width: 80px; font-family: Arial"
class="textbox_numeric" value='<%# Eval("ColumnB", "{0:#,##0}") %>' onkeyup="this.value=FormatNumber(this.value);"
onclick="return this.focus();" onfocus="this.select();"
autocomplete="Off" runat="server" />
</ItemTemplate>
<HeaderTemplate>
Column B</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="60px" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<input id="id3" type="text" name="txt3" style="width: 75px; font-family: Arial"
class="textbox_numeric" value='<%# Eval("ColumnC", "{0:#,##0}") %>' readonly="readonly" onclick="return this.focus();" onfocus="this.select();"
autocomplete="Off" runat="server" />
</ItemTemplate>
<HeaderTemplate>
Column C</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="75px" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<input id="id4" type="text" name="txt4" style="width: 60px; font-family: Arial"
class="textbox_numeric" value='<%# Eval("ColumnD", "{0:#,##0}") %>' onkeyup="this.value=FormatNumber(this.value);"
onchange="" onclick="return this.focus();" onfocus="this.select();" autocomplete="Off"
runat="server" />
</ItemTemplate>
<HeaderTemplate>
Column D</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="60px" />
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="style_header_Grid" />
<RowStyle CssClass="style_item_Grid" />
<PagerStyle CssClass="style_header_Grid" Font-Bold="true" />
</asp:GridView>
I didn't find solution because my english not good to call or describe problem.
Ok, the way to "apporach" this logic and problem?
Process data at the table level in code, and NOT from the gridview (which is for display of data.
So Logic:
Load the data into a table.
Create a 2nd table, and process above rows into ONE row in this 2nd table.
Send 2nd table to GridView.
So, say this markup
<asp:Button ID="Button1" runat="server" Text="Load Grid" CssClass="btn"
OnClick="Button1_Click"
/>
<br />
<br />
<h3>Original table data</h3>
<asp:GridView ID="GridView1" runat="server" CssClass="table"
Width="30%" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="LastName"/>
<asp:BoundField DataField="City"/>
<asp:BoundField DataField="HotelName"/>
</Columns>
</asp:GridView>
<br />
<h3>Crunched table data</h3>
<asp:GridView ID="GridView2" runat="server" CssClass="table"
Width="30%" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="LastName" HtmlEncode="False" />
<asp:BoundField DataField="City" HtmlEncode="False" />
<asp:BoundField DataField="HotelName" HtmlEncode="False" />
</Columns>
</asp:GridView>
And code to load and process would be this:
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim strSQL As String =
"SELECT LastName, City, HotelName FROM tblHotels
WHERE ID IN (127, 120, 124)"
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL As New SqlCommand(strSQL, conn)
Dim rstSource As New DataTable
Dim rstCrunched As New DataTable
conn.Open()
rstSource.Load(cmdSQL.ExecuteReader)
GridView1.DataSource = rstSource ' show origional data
GridView1.DataBind()
For Each MyColumn In rstSource.Columns
rstCrunched.Columns.Add(MyColumn.ColumnName)
Next
Dim ResultRow As DataRow = rstCrunched.NewRow
For Each OneRow As DataRow In rstSource.Rows
For Each MyColumn As DataColumn In rstSource.Columns
If ResultRow(MyColumn.ColumnName).ToString <> "" Then
ResultRow(MyColumn.ColumnName) &= "<br/>"
End If
ResultRow(MyColumn.ColumnName) &= OneRow(MyColumn.ColumnName)
Next
Next
rstCrunched.Rows.Add(ResultRow)
GridView2.DataSource = rstCrunched ' show crunched data
GridView2.DataBind()
End Using
End Using
End Sub
And result is this when button clicked
However, there is a LOT of issues here.
If you using bound fields, then above HtmlEncode="False" is required.
however, if you using lables, (in templated column), then again above is fine.
If you using text boxes, then this:
ResultRow(MyColumn.ColumnName) &= vbCrLf
So, either way, pre-processing of the data is quite much the road to take.
Related
Aim: Gridview with checkbox on each line, user then clicks a button and all ticked lines are actioned in the button code. I need the ID's of all the records where the CheckBox is checked.
Error:
Object reference not set to an instance of an object.
on row: ID_Current = row.FindControl("ID").ToString
if I change the to ID_Current = DirectCast(row.FindControl("cbSelect"), CheckBox).Checked I get 'True' as a result but I already know that and want to get the ID.
Aspx-Code with my Gridview:
<asp:Panel ID="ActionGrid" runat="Server">
<h2>Actions To Edit</h2>
<asp:GridView ID="GridView3" runat="server" AllowPaging="True" AllowSorting="True" AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="False" CssClass="mGrid" DataKeyNames="UPRN" DataSourceID="SqlDataSource3" EmptyDataText="There are no data records to display." Font-Size="Medium" PagerStyle-CssClass="pgr" Width="1000px">
<%-- AutoGenerateEditButton="True"--%>
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server" AutoPostBack="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ControlStyle-Width="50px" DataField="UPRN" HeaderText="UPRN" ReadOnly="True" SortExpression="UPRN" />
<asp:BoundField DataField="LocationItemPosition" HeaderText="Location Item Position" ReadOnly="True" SortExpression="LocationItemPosition" />
<asp:TemplateField HeaderText="Surveye" SortExpression="SurveyDate">
<ItemTemplate>
<asp:Label ID="lblSurveyDate" runat="server" Text='<%# apFunctionCharacters.fncDateTidy(Eval("SurveyDate"))%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ItemRef" HeaderText="Item Ref" ReadOnly="True" SortExpression="ItemRef" />
<asp:BoundField DataField="OverallRiskCategory" HeaderText="Overall Risk Category" ReadOnly="True" SortExpression="OverallRiskCategory" />
<asp:BoundField DataField="Comments" HeaderText="Comments" ReadOnly="True" SortExpression="Comments" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
<asp:Button ID="btnChangeToNA" runat="server" CssClass="Button" Text="Change to NA" />
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
</asp:Panel>
Submit button:
<asp:Button ID="btnChangeToNA" runat="server" CssClass="Button" Text="Change to NA" />
.aspx.vb stored procedure/action code
Protected Sub btnChangeToNA_Click(sender As Object, e As EventArgs) Handles btnChangeToNA.Click
For Each row As GridViewRow In GridView3.Rows
Dim ID_Current As String = ""
'read the label
If DirectCast(row.FindControl("cbSelect"), CheckBox).Checked Then
ID_Current = row.FindControl("ID").ToString
' change value
'############stored procedure here
Dim connection As SqlConnection
Dim command As New SqlCommand
Dim ds As New DataSet
Dim ConnectionString1 As String = System.Configuration.ConfigurationManager.ConnectionStrings("MY_SQL").ToString()
connection = New SqlConnection(ConnectionString1)
connection.Open()
With command
.Connection = connection
.CommandText = "spChangeValue "
.CommandType = CommandType.StoredProcedure
.Parameters.Clear()
.Parameters.AddWithValue("#ID_Current", ID_Current)
.ExecuteNonQuery()
End With
'#############################
lblTest.Text += ID_Current
End If
'Loop
Next
End Sub
With FindControl you find controls(what suprise) not strings. And you have to pass the ID of the control not a property like Id.
So this doesnt make much sense:
Dim ID_Current As String = row.FindControl("ID").ToString()
Instead you could store the ID in a HiddenField, so on aspx:
<asp:HiddenField ID="HiddenID" runat="server" Value='<%# Eval("ID_Current") %>' />
Now you can get the reference to this HiddenField with FindControl:
Dim hiddenID As HiddenField = DirectCast(row.FindControl("HiddenID"), HiddenField)
Dim ID_Current As String = hiddenID.Value
You need a control with the id "ID"
<ItemTemplate>
<asp:HiddenField ID="ID" runat="server" Value='<%# FillMeSomehow%>' />
<asp:CheckBox ID="cbSelect" runat="server" AutoPostBack="false" />
</ItemTemplate>
I have problem this gridview when I click on image button in gridview for delete row. I have this code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Username" HeaderText="Login Name">
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Question" HeaderText="Question">
<ItemStyle Width="200px" />
</asp:BoundField>
<asp:BoundField DataField="Answer" HeaderText="Answer">
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" AutoPostback = "True"
CommandName="Select" ImageUrl="~/images/trash.png" Text="Select" OnClientClick="return confirm('Do you want to delete?');" />
</ItemTemplate>
</asp:TemplateField>
when i click trash image for delete any row it gives me error. Invalid postback or callback argument.
This is my vb.net code
Private Sub updatedata(ByVal sql As String)
connectdb()
cmd = New SqlCommand
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
cmd.ExecuteNonQuery()
conn.Close()
End Sub
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles GridView1.SelectedIndexChanged
Dim sql4 As String = "delete from login WHERE username='" & GridView1.SelectedRow.Cells(0).Text.Trim() & "' "
updatedata(sql4)
I am trying to execute an "INSERT INTO" in the HeaderRow.
The button in the HeaderRow does not trigger the RowCommand.
The gridview contains 5 columns: idt, datetime, col1, col2 and col3.
Protected Sub gvtest_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles gvtest.RowCommand
Dim txt1, txt2, txt3 as String
If e.CommandName = "insertinto" Then
Try
txt1 = DirectCast(gvtest.HeaderRow.FindControl("tbins1"), TextBox).Text
txt2 = DirectCast(gvtest.HeaderRow.FindControl("tbins2"), TextBox).Text
txt3 = DirectCast(gvtest.HeaderRow.FindControl("tbins3"), TextBox).Text
sqltest.InsertParameters("#datetime").DefaultValue = DateTime.Now()
sqltest.InsertParameters("#col1").DefaultValue = txt1
sqltest.InsertParameters("#col2").DefaultValue = txt2
sqltest.InsertParameters("#col3").DefaultValue = txt3
sqltest.Insert()
Catch ex As Exception
TextBox1.Text = "Insert: " & ex.Message
End Try
gvtest.DataBind()
End If
End Sub
default.aspx
<asp:GridView ID="gvtest"
runat="server"
DataSourceID="sqltest"
AutoGenerateColumns="False"
DataKeyNames="idt"
OnRowCommand="gvtest_RowCommand">
<Columns>
<asp:BoundField DataField="idt" HeaderText="idt" Readonly="true" SortExpression="idt" />
<asp:BoundField DataField="datetime" HeaderText="datetime" SortExpression="datetime" />
<asp:TemplateField SortExpression="col1">
<HeaderTemplate>
<asp:TextBox ID="tbins1" text="col1" runat="server" MaxLength="40" />
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="col2">
<HeaderTemplate>
<asp:TextBox ID="tbins2" text="col2" runat="server" MaxLength="40" />
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="col3">
<HeaderTemplate>
<asp:TextBox ID="tbins3" text="col3" runat="server" MaxLength="40" />
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Button ID="btnins" CommandName="insertinto" runat="server" Text="Insert Into" OnClick="btnins_Click" />
</HeaderTemplate>
</asp:GridView>
<asp:SqlDataSource
id="sqltest"
ConnectionString="<%$ ConnectionStrings:connone %>"
SelectCommand="SELECT * FROM [test];
InsertCommand="INSERT INTO [test] datetime,col1,col2,col3 VALUES #datetime,#col1,#col2,#col3;">
</asp:SqlDataSource>
Note for brevity textboxes were omitted containing the gridview data.
You need to use a known command name, like Insert. It will not know your custom command name.
Read this:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.buttonfield.commandname(v=vs.110).aspx
change:
<asp:Button ID="btnins" CommandName="insertinto" runat="server" Text="Insert Into" OnClick="btnins_Click" />
to:
<asp:Button ID="btnins" CommandName="Insert" runat="server" Text="Insert Into" OnClick="btnins_Click" />
GridView row command events come from the Command controls on the data rows.
You placed a button in the GridView header, you need to process the btnins.Command event
I am trying to set the rows colours in the parent grid view base on the data. For example in the coding, if the currency is USD then set the row to chocolate colour else set it to red but instead my program shows that its all in red even though there is currency USD data in the gridview.
heres my code behind,
Protected Sub gvUserInfo_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
e.Row.Cells(1).Visible = False
Dim currency As String = e.Row.Cells(3).Text
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("HedgingTestConnectionString").ConnectionString)
If (e.Row.RowType = DataControlRowType.DataRow) Then
For Each cell As TableCell In e.Row.Cells
If currency = "USD" Then
cell.ForeColor = Color.Chocolate
Else
cell.BackColor = Color.Red
Label1.Text = e.Row.Cells(3).Text
End If
Next
con.Open()
Dim gv As GridView = DirectCast(e.Row.FindControl("gvChildGrid"), GridView)
Dim ref As Integer = Convert.ToInt32(e.Row.Cells(1).Text)
Dim cmd As New SqlCommand("select * from TT where Reference_NO=" & ref, con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
con.Close()
gv.DataSource = ds
gv.DataBind()
End If
End Sub
Below is my gridview code,
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Hedging_ID,CCY" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="SeaGreen" OnRowDataBound="gvUserInfo_RowDataBound" width="100%"
HorizontalAlign="Center" RowStyle-HorizontalAlign="Center" GridLines="None">
<Columns>
<asp:TemplateField ItemStyle-Width="20px">
<ItemTemplate>
<a href="JavaScript:divexpandcollapse('div<%# Eval("ref_ID")%>');">
<img id="imgdiv<%# Eval("ref_ID")%>" border="0" src="plus.png" />
</a>
</ItemTemplate>
<ItemStyle Width="20px"></ItemStyle>
</asp:TemplateField>
<asp:BoundField DataField="ref_ID" HeaderText="ID" SortExpression="ref_ID" />
<asp:BoundField DataField="Bank_Name" HeaderText="Bank" SortExpression="Bank_Name" />
<asp:BoundField DataField="CCY" HeaderText="Currency" SortExpression="CCY" />
<asp:BoundField DataField="Ref_Date" DataFormatString="{0:d}" HeaderText="Contract Date" SortExpression="Ref_Date" />
<asp:BoundField DataField="Ref_End_Date" DataFormatString="{0:d}" HeaderText="Maturity Date" SortExpression="Ref_End_Date" />
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="div<%# Eval("Hedging_ID")%>" style="display: none; position: relative; left: 15px; overflow: auto">
<asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" BorderStyle="Double" BorderColor="#df5015" GridLines="None" Width="700px" HorizontalAlign="Center">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<RowStyle BackColor="#E1E1E1" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:BoundField DataField="Invoice_Number" HeaderText="Invoice Number" SortExpression="Invoice_Number" />
<asp:BoundField DataField="Payment_Date" DataFormatString="{0:d}" HeaderText="Payment Date" SortExpression="Payment_Date" />
<asp:BoundField DataField="Payment_Amount" DataFormatString="{0:c2}" HeaderText="Payment Amount" SortExpression="Payment_Amount" />
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="SeaGreen" Font-Size="Medium"></HeaderStyle>
<RowStyle HorizontalAlign="Center"></RowStyle>
</asp:GridView>
Change the condition like this
If currency = "USD" Then
cell.BackColor= Color.Chocolate
Else
cell.BackColor = Color.Red
Label1.Text = e.Row.Cells(3).Text
End If
May be thats because you have put "ForeColor"
I have checked all available articles and none seem to help me.
I have a GridView that is located inside a Repeater control. The data that populates the Gridview is dynamic and is grouped by a SectionID. I need for each repeated GridView to list the data rows grouped according to their SectionID.
How do I do this?
Thanks.
Here is what I have so far:
Public Sub GrabRepeaterData()
Dim connstr As String = ConfigurationManager.ConnectionStrings("MyString").ToString()
Dim ss As New SqlConnection(connstr)
Dim sqlStr As String = "SELECT SectionID, Name FROM Table1"
Dim selectCMD As New SqlClient.SqlCommand(sqlStr, ss)
Dim dt As New DataTable
Dim ds As New DataSet
Dim dataAdapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter
dataAdapter.SelectCommand = selectCMD
dataAdapter.Fill(dt)
selectCMD.Dispose()
dataAdapter.Dispose()
ss.Close()
ss.Dispose()
dt.Columns.Add("Column1")
dt.Columns.Add("Column2")
dt.Columns.Add("Column3")
dt.Columns.Add("Column4")
dt.Columns.Add("Column5")
dt.Columns.Add("Column6")
dt.Columns.Add("Column7")
dt.Columns.Add("Column8")
For Each row As DataRow In dt.Rows
Dim SectionID As String = ""
SectionID = row("SectionID")
Dim Column1 As String = ""
Dim Column2 As String = ""
Dim Column3 As Boolean
Dim Column4 As String = ""
Dim Column5 As String = ""
Dim Column6 As String = ""
Dim Column7 As Boolean
Dim Column8 As Boolean
Dim ProgConn As String = ""
ProgConn = ConfigurationManager.ConnectionStrings("MyString").ToString()
Dim ProgSqlQuery As New SqlConnection(ProgConn)
Dim ProgResults As New SqlCommand("SELECT [Column1], [Column2], [Column3], [Column4], [Column5], [Column6], [Column7], [Column8] FROM Table2 WHERE SectionID = #SectionID ORDER BY Column4 DESC", ProgSqlQuery)
ProgResults.Parameters.AddWithValue("#SectionID", SectionID).Value = SectionID
ProgSqlQuery.Open()
Dim rProg As SqlDataReader = ProgResults.ExecuteReader()
While rProg.Read()
If Not rProg("Column1").Equals(DBNull.Value) Then
Column1 = CStr(rProg("Column1"))
End If
If Not rProg("Column2").Equals(DBNull.Value) Then
Column2 = CStr(rProg("Column2"))
End If
If Not rProg("Column3").Equals(DBNull.Value) Then
Column3 = CStr(rProg("Column3"))
End If
If Not rProg("Column4").Equals(DBNull.Value) Then
Column4 = CStr(rProg("Column4"))
End If
If Not rProg("Column5").Equals(DBNull.Value) Then
Column5 = CStr(rProg("Column5"))
End If
If Not rProg("Column6").Equals(DBNull.Value) Then
Column6 = CStr(rProg("Column6"))
End If
If Not rProg("Column7").Equals(DBNull.Value) Then
Column7 = CStr(rProg("Column7"))
End If
If Not rProg("Column8").Equals(DBNull.Value) Then
Column8 = CStr(rProg("Column8"))
End If
End While
rProg.Close()
ProgResults.Dispose()
ProgSqlQuery.Close()
ProgSqlQuery.Dispose()
row("Column1") = Column1
row("Column2") = Column2
row("Column3") = Column3
row("Column4") = Column4
row("Column5") = Column5
row("Column6") = Column6
row("Column7") = Column7
row("Column8") = Column8
row.EndEdit()
dt.AcceptChanges()
Next
CustomInfoRepeater.DataSource = dt
CustomInfoRepeater.DataBind()
End Sub
Protected Sub CustomInfoRepeater_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles CustomInfoRepeater.ItemDataBound
If e.Item.ItemType = ListItemType.Item Then
Try
Dim grdVw As GridView = TryCast(e.Item.FindControl("CustomInfoGridView"), GridView)
grdVw.DataSource = DirectCast(e.Item.DataItem, DataTable).Rows
grdVw.DataBind()
Dim CustPanel As UpdatePanel = DirectCast(CustomInfoRepeater.Items(0).FindControl("CustomInfoPanel"), UpdatePanel)
CustPanel.Update()
Catch
End Try
End If
End Sub
Here is my HTML:
<asp:Repeater ID="CustomInfoRepeater" runat="server">
<ItemTemplate>
<div class="download-box3">
</h2>
<asp:UpdatePanel ID="CustomInfoPanel" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:PostBackTrigger ControlID="CustomInfoGridView" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="CustomInfoGridView" runat="server" Width="100%" AutoGenerateColumns="False"
DataKeyNames="Column1" GridLines="None" RowStyle-Height="40px" EnableViewState="False"
CellPadding="4" ForeColor="#333333" RowStyle-VerticalAlign="Middle">
<Columns>
<asp:BoundField DataField="Column1" />
<asp:TemplateField ItemStyle-Width="35px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:ImageButton ID="newIcon" runat="server" Width="31px" CssClass="myGridImage"
Visible="false" ImageUrl="images/new.png" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="35px" />
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="30px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:ImageButton ID="DeleteButton" runat="server" AlternateText="Delete" ImageUrl="images/xmark.png"
OnClientClick="return confirm('Are you sure you want to delete this entry?')"
CommandArgument='<%# Eval("Column1") %>' CommandName="Remove" CssClass="myGridImage">
</asp:ImageButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="30px" />
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:LinkButton ID="GenComUpdateButton" runat="server" CssClass="buttonsmall" Text="update"
CommandArgument='<%# Eval("Column1") %>' CommandName="GenComments"></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Impact" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="30px"
ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:Image ID="statusIcon" runat="server" Width="20px" CssClass="myGridImage" Visible="false"
ImageUrl="" AlternateText="*" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="50px" HeaderText="Files" ItemStyle-HorizontalAlign="Center"
ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:ImageButton ID="filesIcon" runat="server" Width="23px" CssClass="myGridImage"
CommandArgument='<%# Eval("Column3") %>' CommandName="Download" Visible="false"
ImageUrl="images/pdf.png" AlternateText="*" />
<asp:LinkButton ID="AttachAssetsBtn" runat="server" CssClass="buttonredsmall" Text="upload"
CommandArgument='<%# Eval("Column1") %>' CommandName="Uploads" Visible="false"></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
</asp:TemplateField>
<asp:BoundField DataField="Column3" HeaderText="Summary" HeaderStyle-HorizontalAlign="Center"
ItemStyle-VerticalAlign="Middle">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle VerticalAlign="Middle" HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField ItemStyle-Width="50px" HeaderText="Reviewed" ItemStyle-HorizontalAlign="Center"
ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:Image ID="reviewedIcon" runat="server" Width="23px" CssClass="myGridImage" Visible="false"
ImageUrl="images/checkmark.png" AlternateText="*" />
<asp:CheckBox ID="reviewedCheckBox" runat="server" CssClass="inputtext" Visible="false"
AutoPostBack="true" OnCheckedChanged="GenComCheckUpdate" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
</asp:TemplateField>
<asp:BoundField DataField="Column4" HeaderText="Updated" HeaderStyle-HorizontalAlign="Center"
ItemStyle-VerticalAlign="Middle" ItemStyle-Width="75px">
<ItemStyle VerticalAlign="Middle" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Column6" Visible="false" />
<asp:BoundField DataField="Column7" Visible="false" />
</Columns>
<EmptyDataTemplate>
<span class="data-none">No Comments have been added to this section.</span>
</EmptyDataTemplate>
<RowStyle CssClass="RowStyle" BackColor="#F7F6F3" ForeColor="#333333" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<PagerStyle CssClass="PagerStyle" BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle CssClass="SelectedRowStyle" BackColor="#E2DED6" Font-Bold="True"
ForeColor="#333333" />
<HeaderStyle CssClass="HeaderStyle" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle CssClass="EditRowStyle" BackColor="#999999" />
<AlternatingRowStyle CssClass="AltRowStyle" BackColor="White" ForeColor="#284775" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:Repeater>
Thanks for your help!
What I understand from the code is you are merging two datasources into one. Then binding it to the Repeater and then fetching the datasource for GV from the repeater DataItem and binding it to the GridView.
What I would suggest is keep the datasources separate and do it in two steps:
1: Bind the repeater to just the SectionID i.e. data returned by SELECT SectionID, Name FROM Table1
2: In the Repeater ItemDataBound, get the SectionID and fetch data with the query you have in ProgResults. Then bind that to the GV.
I totally agree with gbs. Here's how you can do it:
In the markup, add a hidden field inside repeater, outside the updatepanel:
<asp:Repeater ID="CustomInfoRepeater" runat="server">
<ItemTemplate>
<asp:HiddenField ID="hdnSectionID" Value='<%# Eval("SectionID")%>' runat="server" />
<asp:Label ID="lblName" Text='<%# Eval("Name")%>' runat="server"></asp:Label>
<div class="download-box3">
</h2>
<asp:UpdatePanel ID="CustomInfoPanel" runat="server" UpdateMode="Conditional">
<%-- Rest of the markup goes here --%>
Rewrite GrabRepeaterData() sub only to populate the repeater:
Public Sub GrabRepeaterData()
Dim connstr As String = ConfigurationManager.ConnectionStrings("MyString").ToString()
Dim dt As New DataTable
Using conn As SqlConnection = New SqlConnection(connstr)
conn.Open()
Dim sqlStr As String = "SELECT SectionID, Name FROM Table1"
Dim cmd As New SqlClient.SqlCommand(sqlStr, conn)
Dim adapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter
adapter.SelectCommand = cmd
adapter.Fill(dt)
End Using
CustomInfoRepeater.DataSource = dt
CustomInfoRepeater.DataBind()
End Sub
In repeater's ItemDataBound, find the HiddenField, find it's value, find the GridView inside UpdatePanel, populate it this way:
Protected Sub CustomInfoRepeater_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles CustomInfoRepeater.ItemDataBound
If e.Item.ItemType = ListItemType.Item Then
Dim sectionID As Integer = 0
Dim hdnSectionID = TryCast(e.Item.FindControl("hdnSectionID"), HiddenField)
Dim CustomInfoPanel = TryCast(e.Item.FindControl("CustomInfoPanel"), UpdatePanel)
If (CustomInfoPanel IsNot Nothing AndAlso hdnSectionID IsNot Nothing AndAlso Integer.TryParse(hdnSectionID.Value, sectionID)) Then
Dim CustomInfoGridView = TryCast(CustomInfoPanel.FindControl("CustomInfoGridView"), GridView)
If (CustomInfoGridView IsNot Nothing) Then
Dim connstr As String = ConfigurationManager.ConnectionStrings("MyString").ToString()
Dim dt As New DataTable
Using conn As SqlConnection = New SqlConnection(connstr)
conn.Open()
Dim cmd As New SqlCommand("SELECT [Column1], [Column2], [Column3], [Column4], [Column5], [Column6], [Column7], [Column8] FROM Table2 WHERE SectionID = #SectionID ORDER BY Column4 DESC", conn)
cmd.Parameters.AddWithValue("#SectionID", sectionID)
Dim adapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter
adapter.SelectCommand = cmd
adapter.Fill(dt)
End Using
CustomInfoGridView.DataSource = dt
CustomInfoGridView.DataBind()
End If
End If
End If
End Sub