// 1st GridView /////////////////////////
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" Caption="1st Grid: Simple GridView with SqlDataSource"
AllowPaging="True" AllowSorting="True" AutoGenerateEditButton="true"
DataKeyNames="AutoID" PageSize="8">
<Columns>
<asp:TemplateField HeaderText="Delete?">
<ItemTemplate>
<asp:LinkButton ID="lnk1" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure to Delete?')" CommandName="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:ConnStr %>'
SelectCommand="Select * FROM SampleForTutorials ORDER BY [Name]"
DeleteCommand="Delete FROM SampleForTutorials WHERE AutoID = @AutoID"
UpdateCommand="UPDATE SampleForTutorials SET Name = @Name, Address = @Address, Phone = @Phone, City = @City WHERE AutoID = @AutoID">
<DeleteParameters>
<asp:Parameter Name="AutoID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="AutoID" Type="Int32" />
<asp:Parameter Name="Name" Type="string" Size="50" />
<asp:Parameter Name="Address" Type="string" Size="200" />
<asp:Parameter Name="Phone" Type="int32" />
<asp:Parameter Name="City" Type="string" Size="20" />
</UpdateParameters>
</asp:SqlDataSource>
// 2nd GridView /////////////////////////
<asp:GridView ID="GridView2" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
Caption="2nd Grid: GridView with CodeBehind Data Bound" AllowPaging="False" AllowSorting="False"
BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">
<FooterStyle BackColor="Tan" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" Font-Bold="True" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
// CODE BEHIND
// Bind the GridView /////////////////////////
private void BindGridView()
{
string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlDataAdapter dAd = null;
DataSet dSet = new DataSet();
try
{
string strSql = "Select * FROM SampleForTutorials ORDER BY [Name]";
dAd = new SqlDataAdapter(strSql, conn);
dAd.Fill(dSet, "SampleTable");
GridView2.DataSource = dSet.Tables["SampleTable"].DefaultView;
GridView2.DataBind();
}
catch (Exception ee)
{
lblError.Text = "Error occured. <br>+" + ee.Message.ToString();
}
finally
{
dSet.Dispose();
dAd.Dispose();
conn.Close();
conn.Dispose();
}
}
|
0 comments:
Post a Comment