İki Tablo İle İlişkili Bir DataSet Oluşturmak

 

Merhaba arkadaşlar, bu makalemizde kullanacağımız dataset ile iki tablomuzu ilişkilendireceğiz. Tablolarımız arasında ilişkilendirmeyi CustomerId sütunu üzerinden yapacağız.

 

Sayfamıza 2 adet GridView ve 1 adet TextBox ve Button ekliyoruz.

 


 

 





Şekil 1



 


WebForm1.aspx.cs

 

using System;

using System.Collections.Generic;

using System.Data;

using System.Diagnostics.Eventing.Reader;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace aspnet_gridview_show_two_tables

 

{

 

public partial class WebForm1 : System.Web.UI.Page

 

{

 

DataSet ds;

DataTable dt1 ;

DataTable dt2 ;

 

protected void Page_Load(object sender, EventArgs e)

 

{

 

ds = new DataSet("CustomerOrders");

dt1 = ds.Tables.Add("Customers");

dt2 = ds.Tables.Add("Orders");

 

dt1.Columns.Add("CustomerID", Type.GetType("System.Int32"));

dt1.Columns.Add("FirstName", Type.GetType("System.String"));

dt1.Columns.Add("LastName", Type.GetType("System.String"));

dt1.Columns.Add("Phone", Type.GetType("System.String"));

dt1.Columns.Add("Email", Type.GetType("System.String"));

 

dt2.Columns.Add("CustomerID", Type.GetType("System.Int32"));

dt2.Columns.Add("OrderID", Type.GetType("System.Int32"));

dt2.Columns.Add("OrderAmount", Type.GetType("System.Double"));

dt2.Columns.Add("OrderDate", Type.GetType("System.DateTime"));

ds.Relations.Add("Cust_Order_Rel", ds.Tables

["Customers"].Columns["CustomerID"], ds.Tables

["Orders"].Columns["CustomerID"]);

 

}

 

int i;

 

protected void Button1_Click(object sender, EventArgs e)

 

{

 

i = Convert.ToInt32(TextBox1.Text);

 

if (i == 1)

 

{

DataRow row = dt1.NewRow();

row["CustomerID"] = 1;

row["FirstName"] = "Bahadir";

row["LastName"] = "Sahin";

row["Phone"] = "111-1111";

row["Email"] = "basahin@hotmail.com";

dt1.Rows.Add(row);

 

row = dt2.NewRow();

row["CustomerID"] = 1;

row["OrderID"] = 23;

row["OrderAmount"] = 1;

row["OrderDate"] = "03/07/2024";

dt2.Rows.Add(row);

 

GridView1.DataSource = ds.Tables["Customers"];

GridView1.DataBind();

GridView2.DataSource = ds.Tables["Orders"];

GridView2.DataBind();

 

}

 

else if (i == 2)

 

{

 

DataRow row2 = dt1.NewRow();

row2["CustomerID"] = 2;

row2["FirstName"] = "Melissa";

row2["LastName"] = "Parker";

row2["Phone"] = "222-2222";

row2["Email"] = "melparker@gmail.com";

dt1.Rows.Add(row2);

 

row2 = dt2.NewRow();

row2["CustomerID"] = 2;

row2["OrderID"] = 34;

row2["OrderAmount"] = 2;

row2["OrderDate"] = "03/07/2024";

dt2.Rows.Add(row2);

 

GridView1.DataSource = ds.Tables["Customers"];

GridView1.DataBind();

GridView2.DataSource = ds.Tables["Orders"];

GridView2.DataBind();

 

          }

      }

   }

}

 

 

WebForm1.aspx

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="aspnet_gridview_show_two_tables.WebForm1" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:TextBox ID="TextBox1" runat="server" Font-Names="Arial" Font-Size="Large"></asp:TextBox>

&nbsp;&nbsp;

            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" Font-Names="Arial" Font-Size="Large" />

            <br />

            <br />

            <asp:GridView ID="GridView1" runat="server" CellPadding="4" Font-Names="Arial" Font-Size="Large" ForeColor="#333333" GridLines="None">

                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

                <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" />

                <SortedAscendingCellStyle BackColor="#E9E7E2" />

                <SortedAscendingHeaderStyle BackColor="#506C8C" />

                <SortedDescendingCellStyle BackColor="#FFFDF8" />

                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />

            </asp:GridView>

            <br />

            <br />

            <asp:GridView ID="GridView2" runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Font-Names="Arial" Font-Size="Large">

                <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />

                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />

                <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />

                <RowStyle BackColor="White" ForeColor="#330099" />

                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />

                <SortedAscendingCellStyle BackColor="#FEFCEB" />

                <SortedAscendingHeaderStyle BackColor="#AF0101" />

                <SortedDescendingCellStyle BackColor="#F6F0C0" />

                <SortedDescendingHeaderStyle BackColor="#7E0000" />

            </asp:GridView>

        </div>

    </form>

</body>

</html>

 

 

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN 

About Bahadır Şahin

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Öne Çıkan Yayın

GridView da Seçili Satırı DetailsView da Göstermek

Merhaba arkadaşlar bu makalemizde GridView nesnesi ile birlikte DetailsView nesanesini birlikte kullanacağız. GridView da seçili satırın de...