|
|
|
 |
 |
Исходник |
 |
|
 |
 |
|
Автор:
|
|
|
Название:
|
Universal DataGrid s Auto-Generation coloums( Reflection ) |
|
Дата:
|
19 January 2005 |
|
Описание: |
у нас есть сомвовох у каждого item есть value вносим все нужные имена класов и взависимости от выбора создаем dataset т.е. получаем все свойства класса по названию и посоединяем к datagrid также в |
| |
Разместить ссылку на этот исходник в форуме вы можете вставив в текст сообщения
следующую строку:
[CODEPOST ID=119]Universal DataGrid s Auto-Generation coloums( Reflection )[/CODEPOST] |
| Оценка: |
Проголосовало 7 посетителей, средняя оценка 3.57 |
| Оценить: |
|
1 namespace site.ApplicationModules
2 {
3 using System;
4 using System.Data;
5 using System.Drawing;
6 using System.Web;
7 using System.Collections;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.HtmlControls;
11 using site.Components;
12 using site.Liquidity;
13 using System.Reflection;
14
15 public class Scr510 : PageControl
16 {
17 protected System.Web.UI.WebControls.DropDownList dl_Tables;
18 protected System.Web.UI.WebControls.LinkButton LinkButton3;
19 protected System.Web.UI.WebControls.DataGrid dg_helpTable;
20 protected System.Web.UI.WebControls.Label lbl2;
21 protected System.Web.UI.WebControls.Label lbl3;
22
23 ArrayList header_name=new ArrayList();// array for header name
24 ArrayList key_name=new ArrayList();// array for key in DB
25 const int max_length=250;
26 const int min_lenghth=36;
27
28 public Scr510()
29 {
30 this.Init += new System.EventHandler(Page_Init);
31 }
32
33 private void Page_Init(object sender, EventArgs e)
34 {
35 InitializeComponent();
36 }
37
38 private void Page_Load(object sender, System.EventArgs e)
39 {
40 lbl2.Text="";
41 LinkButton3.Enabled=true;
42 if(!IsPostBack)
43 {
44 dg_helpTable.CssClass=this.gClass;
45 }
46 }
47 #region Web Form Designer generated code
48 /// <summary>
49 /// Required method for Designer support - do not modify
50 /// the contents of this method with the code editor.
51 /// </summary>
52 private void InitializeComponent()
53 {
54 this.dl_Tables.SelectedIndexChanged += new System.EventHandler(this.dl_Tables_SelectedIndexChanged);
55 this.dg_helpTable.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dg_helpTable_ItemCreated);
56 this.dg_helpTable.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dg_helpTable_PageIndexChanged);
57 this.dg_helpTable.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_helpTable_CancelCommand);
58 this.dg_helpTable.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_helpTable_EditCommand);
59 this.dg_helpTable.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_helpTable_UpdateCommand);
60 this.dg_helpTable.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_helpTable_DeleteCommand);
61 this.dg_helpTable.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dg_helpTable_ItemDataBound);
62 this.LinkButton3.Click += new System.EventHandler(this.LinkButton3_Reset);
63 this.Load += new System.EventHandler(this.Page_Load);
64
65 }
66 #endregion
67 private void bindGrid()
68 {
69 lbl3.Text="0";
70 LinkButton3.Visible=true;
71 Cache.Remove("dsCache");
72
73 Type type=Type.GetType(dl_Tables.SelectedValue);
74 Object CL=Activator.CreateInstance(type);
75 MethodInfo mi=type.GetMethod("getRows");
76
77 DataSet ds=(DataSet)mi.Invoke(CL,new object[]{});
78 if(Cache["dsCache"]==null)
79 Cache.Insert("dsCache",ds);
80
81 dg_helpTable.DataSource=ds;
82 dg_helpTable.DataBind();
83 dg_helpTable.Visible=true;
84 }
85
86 private void dl_Tables_SelectedIndexChanged(object sender, System.EventArgs e)
87 {
88 if(dl_Tables.SelectedIndex>-1)
89 {
90 Cache.Remove("dsCache");
91 if(dl_Tables.SelectedIndex!=0)
92 {
93 dg_helpTable.EditItemIndex = -1;
94 dg_helpTable.CurrentPageIndex=0;
95 bindGrid();
96 }
97 else
98 {
99 dg_helpTable.Visible=false;
100 LinkButton3.Visible=false;
101 }
102 }
103 }
104
105 private void dg_helpTable_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
106 {
107 if(lbl3.Text!="1")//if not new row in datagrid
108 dg_helpTable.CurrentPageIndex=e.NewPageIndex;
109 else
110 {
111 if(dg_helpTable.PageCount==2)//for problem with new row if found only 10 row in datagrig
112 {
113 if(Cache["dsCache"]!=null)
114 {
115 DataSet DS=null;
116 DS=(DataSet)Cache["dsCache"];
117 if(DS.Tables[0].Rows.Count==dg_helpTable.PageSize+1)
118 dg_helpTable.CurrentPageIndex=0;
119 else
120 dg_helpTable.CurrentPageIndex=e.NewPageIndex;
121 }
122 else
123 dg_helpTable.CurrentPageIndex=0;
124 }
125 }
126 dg_helpTable.EditItemIndex = -1;
127 bindGrid();
128 }
129
130 private void dg_helpTable_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
131 {
132 dg_helpTable.EditItemIndex = e.Item.ItemIndex;
133 LinkButton3.Enabled=false;
134 bindGrid();
135 }
136
137 private void dg_helpTable_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
138 {
139 bool del_flag=false;//flag=false for normal delete(tablecell) ,flag=true for special delete(textbox)
140
141 if(dg_helpTable.Items.Count==1 && dg_helpTable.CurrentPageIndex>0)//for delete last item in current page
142 dg_helpTable.CurrentPageIndex=--dg_helpTable.CurrentPageIndex;
143
144 if(lbl3.Text=="1" && e.Item.ItemIndex==0)
145 {
146 dg_helpTable.EditItemIndex = -1;
147 bindGrid();
148 }
149 else
150 {
151 if(lbl3.Text=="0")
152 {
153 if(dg_helpTable.EditItemIndex==e.Item.ItemIndex)//for delete row in edit mode
154 del_flag=true;
155 }
156 if(my_method(e.Item.ItemIndex,"Delete",del_flag))
157 lbl2.Text="дозйчд бецтд бдцмзд";
158 else
159 lbl2.Text="дозйчд рлщмд";
160 dg_helpTable.EditItemIndex = -1;
161 }
162 }
163
164 private void dg_helpTable_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
165 {
166 dg_helpTable.EditItemIndex = -1;
167 bindGrid();
168 }
169
170 private void dg_helpTable_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
171 {
172 if(my_method(e.Item.ItemIndex,"Update",true))
173 lbl2.Text="дтглеп бецт бдцмзд";
174 }
175
176 private void dg_helpTable_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
177 {
178 if(e.Item.ItemType==ListItemType.EditItem)
179 {
180 ImageButton b = (ImageButton)e.Item.FindControl("btnDelete");
181 b.Attributes["OnClick"] = "return confirm('?бзшъ мозеч ръерйн ооавш дойгт щм отшлъ држймеъ .дан мдощйк');";
182 e.Item.Cells[0].Wrap=false;
183 key_name.Clear();
184 key_name=ALDUtil.getTableKeys(dl_Tables.SelectedValue.Substring(17));
185 for(int i=2;i<e.Item.Cells.Count;i++)
186 {
187 if(lbl3.Text=="0")//update current row
188 {
189 if(key_name.Contains(header_name[i-2].ToString()) || header_name[i-2].ToString()=="lastUpdated")
190 {
191 ((TextBox)e.Item.Cells[i].Controls[0]).ReadOnly=true;
192 ((TextBox)e.Item.Cells[i].Controls[0]).BorderStyle=BorderStyle.None;
193
194 if(header_name[i-2].ToString()=="lastUpdated")
195 ((TextBox)e.Item.Cells[i].Controls[0]).Text=DateTime.Parse(((TextBox)e.Item.Cells[i].Controls[0]).Text).ToString("d/M/yyyy");
196
197 if((e.Item.ItemIndex%2)!=0)
198 {
199 if(header_name[i-2].ToString()=="lastUpdated")
200 ((TextBox)e.Item.Cells[i].Controls[0]).CssClass="alteritems2";
201 else
202 ((TextBox)e.Item.Cells[i].Controls[0]).BackColor=Color.FromName("#E9EEF2");
203 }
204 else
205 {
206 ((TextBox)e.Item.Cells[i].Controls[0]).BorderColor=Color.White;
207 if(header_name[i-2].ToString()=="lastUpdated")
208 ((TextBox)e.Item.Cells[i].Controls[0]).CssClass="items2";
209 }
210 }
211 else
212 {
213 ((TextBox)e.Item.Cells[i].Controls[0]).BorderColor=Color.White;
214
215 if(header_name[i-2].ToString()=="description")
216 ((TextBox)e.Item.Cells[i].Controls[0]).CssClass="items2";
217 }
218
219 if(header_name[i-2].ToString()=="description")
220 ((TextBox)e.Item.Cells[i].Controls[0]).Width=Math.Max(((TextBox)e.Item.Cells[i].Controls[0]).Text.Length*8,max_length);
221 else
222 ((TextBox)e.Item.Cells[i].Controls[0]).Width=Math.Min(Math.Max(9*Math.Max(header_name[i-2].ToString().Length,((TextBox)e.Item.Cells[i].Controls[0]).Text.Length),min_lenghth),max_length);
223 }
224 else//for new row in datagrid
225 {
226 if(header_name[i-2].ToString()=="lastUpdated")
227 {
228 ((TextBox)e.Item.Cells[i].Controls[0]).BorderColor=Color.White;
229 ((TextBox)e.Item.Cells[i].Controls[0]).CssClass="items";
230 ((TextBox)e.Item.Cells[i].Controls[0]).BorderStyle=BorderStyle.None;
231 ((TextBox)e.Item.Cells[i].Controls[0]).Text=DateTime.Now.ToString("d/M/yyyy");
232 ((TextBox)e.Item.Cells[i].Controls[0]).ReadOnly=true;
233 ((TextBox)e.Item.Cells[i].Controls[0]).Width=Math.Min(((TextBox)e.Item.Cells[i].Controls[0]).Text.Length*9,max_length);
234 }
235 else
236 {
237 if(header_name[i-2].ToString()=="description")
238 {
239 ((TextBox)e.Item.Cells[i].Controls[0]).Width=max_length;
240 ((TextBox)e.Item.Cells[i].Controls[0]).CssClass="items2";
241 }
242 else
243 ((TextBox)e.Item.Cells[i].Controls[0]).Width=Math.Min(Math.Max(header_name[i-2].ToString().Length*9,min_lenghth),max_length);
244 }
245 }
246 }
247 }
248 if(e.Item.ItemType==ListItemType.AlternatingItem || e.Item.ItemType==ListItemType.Item)
249 {
250 ImageButton b = (ImageButton)e.Item.FindControl("btnDelete");
251 b.Attributes["OnClick"] = "return confirm('?бзшъ мозеч ръерйн ооавш дойгт щм отшлъ држймеъ .дан мдощйк');";
252
253 for(int i=2;i<e.Item.Cells.Count;i++)
254 {
255 if((header_name[i-2].ToString()=="lastUpdated" || header_name[i-2].ToString()=="description" || header_name[i-2].ToString()=="equation") && ((TableCell)e.Item.Cells[i]).Text!=" ")
256 {
257 if(header_name[i-2].ToString()=="lastUpdated")
258 ((TableCell)e.Item.Cells[i]).Text=DateTime.Parse(((TableCell)e.Item.Cells[i]).Text).ToString("d/M/yyyy");
259
260 e.Item.Cells[i].Controls.Clear();
261 e.Item.Cells[i].Controls.AddAt(0,new TextBox());
262 ((TextBox)e.Item.Cells[i].Controls[0]).Text=((TableCell)e.Item.Cells[i]).Text;
263 ((TextBox)e.Item.Cells[i].Controls[0]).BorderStyle=BorderStyle.None;
264 ((TextBox)e.Item.Cells[i].Controls[0]).ReadOnly=true;
265
266 if(header_name[i-2].ToString()=="lastUpdated")
267 ((TextBox)e.Item.Cells[i].Controls[0]).Width=Math.Min(((TextBox)e.Item.Cells[i].Controls[0]).Text.Length*9,max_length);
268 else
269 {
270 if(header_name[i-2].ToString()=="equation")
271 {
272 if(((TextBox)e.Item.Cells[i].Controls[0]).Text.Length*8>max_length)
273 {
274 ((TextBox)e.Item.Cells[i].Controls[0]).Width=max_length;
275 ((TextBox)e.Item.Cells[i].Controls[0]).TextMode=TextBoxMode.MultiLine;
276 }
277 else
278 ((TextBox)e.Item.Cells[i].Controls[0]).Width=((TextBox)e.Item.Cells[i].Controls[0]).Text.Length*8;
279 }
280 else
281 ((TextBox)e.Item.Cells[i].Controls[0]).Width=((TextBox)e.Item.Cells[i].Controls[0]).Text.Length*8;
282 }
283
284 if((e.Item.ItemIndex%2)!=0)
285 ((TextBox)e.Item.Cells[i].Controls[0]).CssClass="alteritems2";
286 else
287 {
288 ((TextBox)e.Item.Cells[i].Controls[0]).BorderColor=Color.White;
289 ((TextBox)e.Item.Cells[i].Controls[0]).CssClass="items2";
290 }
291 }
292 else
293 ((TableCell)e.Item.Cells[i]).Width=Math.Min(((TableCell)e.Item.Cells[i]).Text.Length*9,max_length);
294 }
295 }
296 }
297 private void dg_helpTable_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
298 {
299
300 if ( e.Item.ItemType == ListItemType.Header )
301 {
302 header_name.Clear();
303 for(int i=2;i<e.Item.Cells.Count;i++)
304 header_name.Add(e.Item.Cells[i].Text);
305 }
306 }
307
308 private void LinkButton3_Reset(object sender, System.EventArgs e)
309 {
310 //adding empty row in dataset for input new data
311 if(dg_helpTable.EditItemIndex==-1)
312 {
313 lbl3.Text="1";//flag for new row in dataset
314 LinkButton3.Visible=false;
315 dg_helpTable.EditItemIndex = 0;
316 dg_helpTable.CurrentPageIndex=0;
317 DataSet DS=null;
318
319 if(Cache["dsCache"]==null)
320 {
321 Type type=Type.GetType(dl_Tables.SelectedValue);
322 Object CL=Activator.CreateInstance(type);
323 MethodInfo mi=type.GetMethod("getRows");
324 DS=(DataSet)mi.Invoke(CL,new object[]{});
325 }
326 else
327 DS=(DataSet)Cache["dsCache"];
328
329 DataRow dr=DS.Tables[0].NewRow();
330 DS.Tables[0].Rows.InsertAt(dr,0);
331 DS.AcceptChanges();
332
333 dg_helpTable.DataSource=DS;
334 dg_helpTable.DataBind();
335 dg_helpTable.Visible=true;
336 }
337 }
338 private bool my_method(int index,string command,bool flag)
339 { //index - row for command ,command - name command ,flag - for know about item control(tablecell/textbox)
340 DataGridItem dgi=dg_helpTable.Items[index];
341 ArrayList errorList=new ArrayList();
342
343 key_name.Clear();
344 key_name=ALDUtil.getTableKeys(dl_Tables.SelectedValue.Substring(17));
345
346 Type type=Type.GetType(dl_Tables.SelectedValue);
347 Object CL=Activator.CreateInstance(type);
348 string[] prop=new string[type.GetProperties().Length];
349 object temp;
350
351 for(int i=2;i<dgi.Cells.Count;i++)
352 {
353 PropertyInfo pi=type.GetProperty(header_name[i-2].ToString());
354
355 if(header_name[i-2].ToString()!="lastUpdated")
356 {
357 try
358 {
359 if(flag)
360 temp=Convert.ChangeType(((TextBox)dgi.Cells[i].Controls[0]).Text,pi.PropertyType);
361 else
362 temp=Convert.ChangeType(dgi.Cells[i].Text,pi.PropertyType);
363
364 pi.SetValue(CL,temp,null);
365 prop[i-2]=temp.ToString();
366 }
367 catch{prop[i-2]=((TextBox)dgi.Cells[i].Controls[0]).Text;}
368 }
369 else
370 {
371 pi.SetValue(CL,DateTime.Now,null);
372 prop[i-2]=DateTime.Now.ToString();
373 }
374 }
375 MethodInfo mi=type.GetMethod(command);
376
377 if(command=="Update")
378 errorList=ALDUtil.setTableObject(ref CL,prop);
379
380 if(errorList.Count==0)
381 {
382 bool tmp=false;
383 if((int)mi.Invoke(CL,new object[]{})<0)
384 {
385 mi=type.GetMethod("makeNote");
386 if(CLAlerts.makeAlert(ALDUtil.getCalcDate(),104,CL.GetType().Name+(string)mi.Invoke(CL,new object[]{}))<0)//necessary to add alert for update fail
387 lbl2.Text="чййоъ бтйд шцйрйъ босг дръерйн ра мфреъ мордм дъфтем";
388 else
389 lbl2.Text="дтглеп ма дцмйз бтйд босг дръерйн";
390 tmp=false;
391 }
392 else
393 tmp=true;
394
395 dg_helpTable.EditItemIndex = -1;
396 bindGrid();
397
398 return tmp;
399 }
400 else
401 {
402 CLAuxTables aux=new CLAuxTables();
403 string propName="";
404 string alertType="";
405 LinkButton3.Visible=false;
406
407 foreach(string[] error in errorList)
408 {
409 propName+=error[0]+" ,";
410 alertType=error[1];
411 }
412
413 aux.tableId=804;
414 aux.valuet=Int32.Parse(alertType)+100;
415 if(aux.setTableObject()<0)
416 {
417 if(CLAlerts.makeAlert(ALDUtil.getCalcDate(),105,"AuxTables "+aux.makeNote())<0)//necessary to add alert for update fail
418 lbl2.Text="чййоъ бтйд шцйрйъ босг дръерйн ра мфреъ мордм дъфтем";
419 else
420 lbl2.Text="чййоъ бтйд босг дръерйн";
421 }
422 else
423 lbl2.Text=aux.description+" "+propName.Substring(0,propName.Length-1);
424
425 return false;
426 }
427 }
428 }
429 } |
| Вернуться к списку исходников в категории DataGrid, DataList, Repeater |
|
|
 |
 |
 |
 |
|
|