Theory
Bill को एक Bill जैसा दिखना ज़रूरी है
अब तक ShopKeeper हर added item को एक Label में report करता है: एक line, अगली से overwritten। Mehta Uncle अपने पुराने paper pad की तरफ़ point करते हैं: item, quantity, amount, ROWS में, सब कुछ visible रहते हुए जब तक bill पूरा न हो।
Till को जो चाहिए वह है screen पर एक table। .NET इस need को 2 halves में 2 names के साथ बाँटता है: data की rows HOLD करने के लिए कुछ, और उन्हें SHOW करने के लिए कुछ। 2 halves को confuse करना Unit 3 की classic mistake है, तो आज हम दोनों बनाते हैं।
Theory
Register और Glass Counter
Shop की register book दिन की entries hold करती है: ruled columns, एक sale के लिए एक row। यह DATA है, और यह exist करती है चाहे कोई देखे या नहीं।
Glass display counter चीज़ें SHOW करता है: जो कुछ भी आप अंदर रखें visible हो जाता है, इसके frame में arranged।
एक DataSet register है: memory में रहने वाली rows की tables। एक DataGrid glass है: एक control जो जो भी table आप इसमें रखें display करता है। Glass के बिना register: data जो कोई नहीं देखता। Register के बिना glass: एक empty frame।
Theory
DataSet और DataGrid, Formally
एक DataSet DataTables का एक in-memory container है (यह कई hold कर सकता है, plus इनके बीच relations)। हर DataTable के पास typed Columns और data Rows हैं। सबसे ज़रूरी बात यह disconnected है: इसे कोई database नहीं चाहिए, कोई connection नहीं, सिर्फ़ RAM। आज ShopKeeper इसे code से fill करता है; Unit 5 में एक DataAdapter इसी structure को एक असली database से fill करेगा।
एक DataGrid (नया Visual Studio इसे DataGridView कहता है) display control है: इसकी DataSource property को एक table पर point कीजिए, और rows automatically एक grid की तरह render होते हैं।
Practical
Bill: Table बनाइए, 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
ShopKeeper run होते हुए एक DataSet का data असल में कहाँ रहता है?
- Memory (RAM) में, कोई database या connection ज़रूरी नहीं
- इसे display करने वाले DataGrid control में
- एक temporary file में जो Visual Studio disk पर बनाता है
- Database में; DataSet सिर्फ़ इस पर एक live window है
Show the answer
Memory (RAM) में, कोई database या connection ज़रूरी नहीं
DataSet एक in-memory, DISCONNECTED store है: हमारी listing कोसों दूर तक कोई database न होने के बावजूद एक बनाती और fill करती है। Option B register-and-glass split को उल्टा कर देता है: grid सिर्फ़ display करता है; grid delete कीजिए और data untouched survive करता है। Option C ऐसी plumbing invent करता है जो exist नहीं करती। Option D एक connected model describe करता है, और exactly उस word में ग़लत है जो matter करता है: जब Unit 5 भी एक database SE एक DataSet fill करती है, copy फिर independently RAM में रहती है, changes सहित, जब तक आप इन्हें वापस push न करें। वही disconnected nature पूरा design है।
Think first
एक Table, दो Glasses
मान लीजिए Day Report tab पर एक दूसरा grid भी DataSource = bill set करता है। Add button fire होता है, 2 rows table में enter होती हैं। Tap करने से पहले: हर grid क्या दिखाता है, और यह क्या prove करता है?
Show the answer
दोनों grids दोनों rows दिखाते हैं, instantly, क्योंकि कोई भी grid data का OWNER नहीं है: हर एक same DataTable का एक view है, और binding का मतलब है display data को जहाँ भी यह बदले follow करता है। यही split का proof है: data एक बार रहता है (register), displays इस पर सस्ती windows हैं (जितनी चाहें उतनी glasses)। यह listing में golden habit भी explain करता है: totals bill.Rows से compute होते हैं, कभी grid cells पढ़कर नहीं, क्योंकि grids आँखों के लिए हैं, tables logic के लिए।
Watch out
Confusions जिनके लिए यह Topic Famous है
DataSet बनाम DataGrid: एक hold करता है, एक show करता है। एक exam sentence में: DataSet एक disconnected in-memory data container है; DataGrid वह control है जो इसे display करता है।
DataSet बनाम DataTable: DataSet file cabinet है, DataTables इसकी drawers हैं। छोटे programs अक्सर एक अकेला DataTable directly bind करते हैं, जैसे हमने किया।
Grid से Compute करना: cell text पढ़कर इसे re-parse करना format bugs invite करता है; इसके बजाय DataTable की Rows loop कीजिए।
Theory
यह Lesson Unit 5 की Rehearsal है
नोटिस कीजिए आज को क्या नहीं चाहिए था: एक database। DataSet family design ही ऐसे हुई थी कि screens दोनों तरह से identically काम करें: आज bill.Rows.Add इसे code से fill करता है; Unit 5 में, एक DataAdapter.Fill(ds) असली saved bills को इसी structure में डालेगा, और dgvBill को फ़र्क पता नहीं चलेगा। अभी register-and-glass split सीखिए और ADO.NET आधा सीखा जा चुका होगा। अगला lesson: component tray के invisible helpers: Timer, ErrorProvider और friends।
Summary
Key takeaways
- DataSet = DataTables का in-memory, disconnected container; DataTable = typed Columns + Rows।
- DataGrid (DataGridView) = display control; एक बार DataSource से bind कीजिए और rows add होते ही दिखते हैं।
- Code में build कीजिए: Columns.Add(name, GetType(...)), Rows.Add(values...)।
- Data और display अलग हैं: कई grids एक table watch कर सकते हैं।
- DataTable की Rows से compute कीजिए, कभी grid cells से नहीं।
- Unit 5 एक DataAdapter से इसी DataSet को एक असली database से fill करती है: screen code unchanged रहता है।
- Memory hook: DataSet register book है, DataGrid glass counter है।