Theory
bill ને bill જેવું look કરવાની જરૂર છે
અત્યાર સુધી ShopKeeper દરેક added item ને Label માં report કરે છે: એક line, next દ્વારા overwritten. Mehta Uncle તેના જૂના paper pad પર points કરે છે: item, quantity, amount, IN ROWS, everything visible સાથે જ્યાં સુધી bill done ન થાય.
till ને જે જોઈએ છે એ table on screen છે. .NET એ need ને 2 halves માં split કરે છે 2 names સાથે: કંઈક rows of data ને HOLD કરવા માટે, અને કંઈક SHOW કરવા માટે. 2 halves ને confuse કરવું એ classic Unit 3 mistake છે, એટલે આજે આપણે બંને ને build કરીએ છીએ.
Theory
register અને glass counter
shop નું register book day ની entries ને hold કરે છે: ruled columns, દરેક sale માટે એક row. એ DATA છે, અને તે exists કરે છે whether or not કોઈ looks કરે છે.
glass display counter THINGS ને SHOW કરે છે: જે કંઈ તમે અંદર મૂકો તે visible બને છે, તેના frame માં arranged.
DataSet એ register છે: tables of rows memory માં living. DataGrid એ glass છે: control જે કોઈ પણ table ને display કરે છે જે તમે તેમાં મૂકો. register without glass: data કોઈ જોતું નથી. glass without register: empty frame.
Theory
DataSet અને DataGrid, ઔપચારિક રીતે
DataSet એ DataTables નો in-memory container છે (તે several ને hold કરી શકે છે, plus તેમની વચ્ચે relations). દરેક DataTable typed Columns અને data Rows ધરાવે છે. Crucially તે disconnected છે: તેને કોઈ database ની જરૂર નથી, કોઈ connection નહીં, કંઈ નહીં પણ RAM નહીં. આજે ShopKeeper તેને code થી fill કરે છે; Unit 5 માં DataAdapter તે very same structure ને real database થી fill કરશે.
DataGrid (newer Visual Studio તેને DataGridView કહે છે) એ display control છે: તેના DataSource property ને table પર point કરો, અને rows automatically grid તરીકે render થાય છે.
Practical
bill: table build કરો, grid bind કરો
Public Class BillingForm
Dim bill As New DataTable("Bill")
Private Sub BillingForm_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
bill.Columns.Add("Item") ' String by default
bill.Columns.Add("Qty", GetType(Integer))
bill.Columns.Add("Amount", GetType(Decimal))
dgvBill.DataSource = bill ' the binding
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) _
Handles btnAdd.Click
bill.Rows.Add("Soap", 2, 80D)
bill.Rows.Add("Tea", 1, 140D) ' grid updates by itself
Dim total As Decimal = 0
For Each r As DataRow In bill.Rows
total += CDec(r("Amount")) ' data, not display
Next
lblTotal.Text = "Total: " & total ' Total: 220
End Sub
End Class
Quiz
DataSet નું data ક્યાં actually live કરે છે જ્યારે ShopKeeper runs કરે છે?
- memory (RAM) માં, કોઈ database અથવા connection જરૂરી નથી
- DataGrid control માં જે તેને display કરે છે
- temporary file માં જે Visual Studio disk પર create કરે છે
- database માં; DataSet એ તેના પર just live window છે
Show the answer
memory (RAM) માં, કોઈ database અથવા connection જરૂરી નથી
DataSet એ in-memory, DISCONNECTED store છે: આપણી listing એ એક ને build અને fill કરે છે કોઈ database વિના miles માં. Option B register-and-glass split ને invert કરે છે: grid ફક્ત display કરે છે; grid ને delete કરો અને data untouched survive કરે છે. Option C plumbing ને invent કરે છે જે exist નથી કરતું. Option D connected model ને describe કરે છે, અને exactly wrong છે તે word માં જે matters કરે છે: even જ્યારે Unit 5 DataSet ને database થી fill કરે છે, copy પછી independently RAM માં lives કરે છે, changes અને all, જ્યાં સુધી તમે તેમને back push ન કરો. એ disconnected nature એ whole design છે.
Think first
એક table, બે glasses
ધારો કે Day Report tab પર બીજું grid પણ DataSource = bill set કરે છે. Add button fire થાય છે, 2 rows table માં enter થાય છે. tap કરતા પહેલા: દરેક grid શું show કરે છે, અને એ શું prove કરે છે?
Show the answer
બંને grids બંને rows ને show કરે છે, instantly, કારણ કે કોઈ grid data ને OWN નથી કરતું: દરેક same DataTable નો view છે, અને binding એટલે display data ને follow કરે છે જ્યાં પણ તે changes થાય છે. એ split નો proof છે: data એકવાર lives કરે છે (register), displays તેના પર cheap windows છે (જેટલા glasses ગમો તેટલા). એ listing માં golden habit ને પણ explain કરે છે: totals ને bill.Rows થી compute કરવામાં આવે છે, ક્યારેય grid cells ને reading થી નહીં, કારણ કે grids eyes માટે છે, tables logic માટે છે.
Watch out
confusions જે આ topic famous છે
DataSet vs DataGrid: એક hold કરે છે, એક show કરે છે. exam sentence માં: DataSet એ disconnected in-memory data container છે; DataGrid એ control છે જે તેને display કરે છે.
DataSet vs DataTable: DataSet એ file cabinet છે, DataTables એ તેના drawers છે. small programs ઘણીવાર lone DataTable ને directly bind કરે છે, જેમ આપણે કર્યું.
grid થી computing: cell text ને reading અને re-parsing કરવું format bugs ને invite કરે છે; DataTable ના Rows ને loop કરો instead.
Theory
આ lesson એ Unit 5 છે rehearsal માં
notice કરો કે આજે શું DID NOT need: database. DataSet family એ રીતે design થઈ હતી કે screens identically work કરે either way: આજે bill.Rows.Add તેને code થી fill કરે છે; Unit 5 માં, એક DataAdapter.Fill(ds) real saved bills ને same structure માં pour કરશે, અને dgvBill ને difference નહીં ખબર પડે. register-and-glass split ને હવે learn કરો અને ADO.NET અડધું-learnt બને છે. next lesson: component tray માં invisible helpers: Timer, ErrorProvider અને friends.
Summary
Key takeaways
- DataSet = in-memory, disconnected container of DataTables; DataTable = typed Columns + Rows.
- DataGrid (DataGridView) = display control; DataSource સાથે once bind કરો અને rows added થાય તેમ appear થાય છે.
- code માં build કરો: Columns.Add(name, GetType(...)), Rows.Add(values...).
- Data અને display separate છે: many grids એક table ને watch કરી શકે છે.
- DataTable ના Rows થી compute કરો, ક્યારેય grid cells થી નહીં.
- Unit 5 same DataSet ને real database થી DataAdapter દ્વારા fill કરે છે: screen code unchanged રહે છે.
- Memory hook: DataSet એ register book છે, DataGrid એ glass counter છે.