Binding data from one GridView to anther GridView after selecting some rows in the first GridView
Here is an example of binding data from one GridView
to anther GridView
after selecting some rows in the first GridView
. First, we will create a GridView
with a checkbox and two buttons in the footer: one for searching and another for adding data from one GridView
to the other.
<asp:GridView ID="grdDocChk" DataKeyNames="DOCCODE"
runat="server" BackColor="White" BorderColor="#CC9966"
BorderStyle="None" BorderWidth="1px"
CellPadding="4" Style="z-index: 110; left: 208px; position: absolute; top: 416px"
Visible="TRUE" AutoGenerateColumns="False" ShowFooter="True"
AllowPaging="True" Height="10px">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True"
ForeColor="#663399" />
<RowStyle BackColor="#EEEEEE" BorderColor="CornflowerBlue"
ForeColor="Black" Height="30px" />
<PagerStyle BackColor="DarkGray" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<Columns >
<asp:TemplateField >
<ItemTemplate ><asp:CheckBox ID="chkBox" runat =" server" /> </ItemTemplate>
<FooterTemplate >
<asp:Label ID="lblDocCd" runat ="server" Text ="Doctor Code" />
<asp:TextBox runat ="server" ID="txtDocCd" />
<asp:Label ID="lblDocNam" Text ="DoctorName" runat ="server" />
<asp:TextBox runat ="server" ID="txtDocnam" />
<asp:Button ID ="cmdSearch" CommandName ="Search"
runat ="server" Text="Search" Width="60" />
<asp:Button ID ="cmdAdd" CommandName ="Add"
runat ="server" Text="Add" Width ="60" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText ="Doctor code" DataField ="DOCCODE" />
<asp:BoundField HeaderText="Doctor name" DataField ="DOCNAME" />
</Columns>
</asp:GridView>
Now create another GridView
without checkbox but containing the same columns.
<asp:GridView ID="grdDocTxt" DataKeyNames="DOCCODE"
runat="server" BackColor="White" BorderColor="#CC9966"
BorderStyle="None" BorderWidth="1px" CellPadding="4"
Style="z-index: 111; left: 696px; position: absolute; top: 512px"
Visible="False" AutoGenerateColumns="False" AllowPaging="True">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True"
ForeColor="#663399" />
<RowStyle BackColor="#EEEEEE" BorderColor="CornflowerBlue"
ForeColor="Black" Height="30px" />
<PagerStyle BackColor="DarkGray" />
<HeaderStyle BackColor="#000084"
Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField headerText="doctor code" DataField ="DOCCODE" />
<asp:BoundField HeaderText="doctor name" DataField ="DOCNAME" />
<asp:TemplateField Headertext="DoctorRole">
<ItemTemplate >
<asp:DropDownList runat="server" ID="cmbDocRole" >
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Surgeon</asp:ListItem>
<asp:ListItem>Asst.Surgeon</asp:ListItem>
<asp:ListItem>Anesthesia</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:templatefield HeaderText ="Charges">
<ItemTemplate >
<asp:TextBox ID="txtChrgs" runat ="server" />
</ItemTemplate>
</asp:templatefield>
</Columns>
</asp:GridView>
In page load, bind data to the GridView
grddocchk
.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack() Then
dim sqlstr as string
sqlstr = "select DOCCODE,DOCNAME from TBL_ADM_MS_DOCTOR where AND DOCACTIVE = 'A'"
binddata(sqlstr)
End Sub
Here is the function for binding data:
private sub bindData(sqlstr as string)
SqlConnection sqlConnection = new SqlConnection ( "connectionString" );
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter ( sqlstr, sqlConnection );
DataSet dataSet = new DataSet ();
GridView1.DataSource = dataSet;
GridView1.DataBind ();
end sub
After clicking the Search button in grddocchk
, the GridView
will be filled with the selection criteria, i.e., depending on the value in the textbox txtdoccode
, txtdocname
GridView
should be filled. If we click the Add button, then rows which have been checked in grddocchk
will be added to grddoctxt
.
Protected Sub gvDocChk_RowCommand(ByVal sender As Object, ByVal e As _
System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvDocChk.RowCommand
If e.CommandName = "Search" Then
sqlstr = "select DOCCODE,DOCNAME from TBL_ADM_MS_DOCTOR where DOCHSPCODE='" & _
gappln_data.appln_gHspCd & "' AND DOCACTIVE = 'A'"
txtdoccd = CType(gvDocChk.FooterRow.FindControl("txtdoccd"), TextBox)
txtdocnam = CType(gvDocChk.FooterRow.FindControl("txtdocnam"), TextBox)
If txtdoccd.Text <> Nothing Then
sqlstr = sqlstr & " AND DOCCODE like'" & Trim$(txtdoccd.Text) & "%'"
bindData(sqlstr)
End If
If txtdocnam.Text <> Nothing Then
sqlstr = sqlstr & "AND DOCNAME like'" & Trim$(txtdocnam.Text) & "%'"
bindData(sqlstr)
End If
If gvDocChk.Rows.Count = 0 Then
MsgBox("record not found", MsgBoxStyle.Information, "MasterHealth")
txtdoccd.Text = ""
txtdocnam.Text = ""
End If
End If
If e.CommandName = "Add" Then
Call add_gvdoc(gvDocChk, gvDocTxt)
gvDocTxt.Visible = True
End If
End Sub
Here is the function for adding a GridView
to another:
Protected Sub add_gvdoc(ByVal gvdocchk, ByVal gvdoctxt)
dtable.Columns.Add(New DataColumn("DOCCODE"))
dtable.Columns.Add(New DataColumn("DOCNAME"))
For Each gvr As GridViewRow In gvdocchk.rows
chek = CType(gvr.Cells(0).FindControl("ChkBox"), CheckBox)
If chek.Checked = True Then
drow = dtable.NewRow()
drow("DOCCODE") = gvr.Cells(1).Text
drow("DOCNAME") = gvr.Cells(2).Text
dtable.Rows.Add(drow)
End If
Next
gvdoctxt.datasource = dtable
gvdoctxt.databind()
gvdoctxt.visible = True
gvdocchk.visible = False
End Sub