Posts Tagged ‘datatable’

ASP.NET Tek DataGrid’de 2 DataTable

Sayfa tasarimi :

Sayfa Tasarimi

Default.aspx.cs kodlarimiz

  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4.  
  5. public partial class _Default : System.Web.UI.Page
  6. {
  7. protected void Page_Load(object sender, EventArgs e)
  8. {
  9. this.TekGrid();
  10. }
  11.  
  12. void TekGrid()
  13. {
  14. string _connectionString = "data source=localhost;initial catalog=AdventureWorks;integrated security=true";
  15.  
  16. SqlConnection _cn1 = new SqlConnection(_connectionString);
  17. SqlConnection _cn2 = new SqlConnection(_connectionString);
  18.  
  19. SqlDataAdapter _adapter1 = new SqlDataAdapter("select ProductID,Name,ListPrice,SafetyStockLevel,Color,ProductSubcategoryID from Production.Product", _cn1);
  20. SqlDataAdapter _adapter2 = new SqlDataAdapter("select ProductSubcategoryID,Name from Production.ProductSubcategory", _cn2);
  21.  
  22. DataTable _dt1 = new DataTable();
  23. DataColumn[] _cl1 = new DataColumn[]
  24. {
  25. new DataColumn("ID",typeof(int))
  26. {
  27. AutoIncrement = true,
  28. Unique = true,
  29. AutoIncrementSeed = 1,
  30. AutoIncrementStep = 1
  31. },
  32. new DataColumn("Name",typeof(string)),
  33. new DataColumn("SurName",typeof(string))
  34. };
  35.  
  36. _dt1.Columns.AddRange(_cl1);
  37.  
  38. DataTable _dt2 = new DataTable();
  39. DataColumn[] _cl2 = new DataColumn[]
  40. {
  41. new DataColumn("ID",typeof(int))
  42. {
  43. AutoIncrement = true,
  44. Unique = true,
  45. AutoIncrementSeed = 1,
  46. AutoIncrementStep = 1
  47. },
  48. new DataColumn("Address",typeof(string)),
  49. new DataColumn("City",typeof(string))
  50. };
  51.  
  52. _dt2.Columns.AddRange(_cl2);
  53.  
  54. string[] _names = new string[]
  55. {
  56. "kenan","myfan","yourfan","irfan","coder","şimdi uzaklardasin"
  57. };
  58.  
  59. string[] _surNames = new string[]
  60. {
  61. "kalfa","dedikodu","liar","pinokyo"
  62. };
  63.  
  64. string[] _adresses = new string[]
  65. {
  66. "moda","yenidogan","tandogan","kordon","maslak"
  67. };
  68.  
  69. string[] _cities = new string[]
  70. {
  71. "istanbul","ankara","izmir"
  72. };
  73.  
  74. Random rnd = new Random();
  75. for (int i = 0; i < 10; i++)
  76. {
  77. DataRow _row = _dt1.NewRow();
  78.  
  79. _row["Name"] = _names[rnd.Next(0, _names.Length)];
  80. _row["SurName"] = _surNames[rnd.Next(0, _surNames.Length)];
  81. _dt1.Rows.Add(_row);
  82. System.Threading.Thread.Sleep(10);
  83. }
  84.  
  85. for (int i = 0; i < 10; i++)
  86. {
  87. DataRow _row = _dt2.NewRow();
  88.  
  89. _row["Address"] = _adresses[rnd.Next(0, _adresses.Length)];
  90. _row["City"] = _cities[rnd.Next(0, _cities.Length)];
  91. _dt2.Rows.Add(_row);
  92. System.Threading.Thread.Sleep(10);
  93. }
  94.  
  95. _dt1.PrimaryKey = new DataColumn[] { _dt1.Columns[0] };
  96. _dt2.PrimaryKey = new DataColumn[] { _dt2.Columns[0] };
  97.  
  98. _dt1.Merge(_dt2, false, MissingSchemaAction.AddWithKey);
  99.  
  100. myGrid.DataSource = _dt1;
  101. myGrid.DataBind();
  102. }
  103. }

Çıktı :

One Grid,Multi DataTable