PDA

View Full Version : ساخت دیتا گرید داخل کلاس



vB.N3T
پنج شنبه 23 خرداد 1392, 19:12 عصر
سلام دوستان چطور میشه تو یه کلاس یه دیتاگرید ساخت و توو فرم ازش نمونه سازی کنی

saied_hacker
جمعه 24 خرداد 1392, 07:41 صبح
سوالت خیلی خلاصه بود و من برداشت کردم فقط میخای با کد یه دیتا گرید بسازی و نمایش بدی....

DataGridView dg = new DataGridView();

dg.Location = new Point(0, 0);
dg.Height = 200;
dg.Width = 400;

this.Controls.Add(dg);


اگر میخای این کارو توی یه کلاس دیگه انجام بدی و توی یه فرم نشونش بدی با یه یه ابجکت از روی کلاس فرمت توی کلاس مورد نظر بسازی و ادرس فرم جاری رو توش ست کنی ....


// myclass.cs

public Form2 set
{
get;
set;
}

//

و در اخری با set ( مثال بالا) که فرم شماست کار کنید.

vB.N3T
جمعه 24 خرداد 1392, 13:56 عصر
ممنون
اما this رو میشناسه اما Controls رو نمیشناسه

عکس زیر رو مشکلشو حل کردم using ها اضافه نشده بودن

ali_md110
جمعه 24 خرداد 1392, 14:12 عصر
سلام
خود دیتاگراید یک کلاس میباشد که میتونید با ارث بری در یک کلاس یک دیتاگراید سفارشی بسازید و بارها استفاده کنید
مثال:
کلاس زیر یک دیتاگراید هست در این دیتاگراید اگر بر روی کلید اینتر کیبورد فشار بدیم فوکوس بجای رفتن به سطر بعدی به سلول بعدی انتقال پیدا میکنه
میتونید سایر متدها و خصوصیتهای دیگه دیتاگراید را تغییر بدید و یک دیتاگراید کاملا خصوصی درست کنید
جهت استفاده اگر یکبار برنامتون اجرا کنید یک شی دیتاگراید به لیست کنترلهاتون در toolbax اضافه میشه میتونید با کشیدن روی فرم از دیتاگراید استفاده کنید روش دیگه اینه که یک نمونه از این کلاس بسازید:


MyGridview grid= new MyGridview ();
this.Controls.Add(grid);


این هم کلاس دیتاگرایدمون:

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
public class MyGridview : DataGridView
{

protected override bool ProcessDialogKey(Keys keyData)
{
// ERROR: Not supported in C#‎: OnErrorStatement

Keys key = (keyData & Keys.KeyCode);
// treat the Enter key as the Tab
if (key == Keys.Enter) {
SendKeys.Send("{Tab}");
return true;
}
if (key == Keys.Tab) {
// try to find the next editable cell. if found, set the current cell to it; otherwise, do nothing
int col = this.CurrentCell.ColumnIndex + 1;
while (col < this.Columns.Count) {
if (!(this.Columns[col].ReadOnly)) {
break; // TODO: might not be correct. Was : Exit Do
}
col += 1;
}
if (col < this.Columns.Count) {
this.CurrentCell = this.Rows[this.CurrentCell.RowIndex].Cells[col];
} else {
if (this.CurrentCell.RowIndex != this.Rows.Count - 1) {
for (col = 0; col <= this.CurrentCell.ColumnIndex; col++) {
if (!(this.Columns[col].ReadOnly)) {
break; // TODO: might not be correct. Was : Exit For
}
}

if (col <= this.CurrentCell.ColumnIndex) {
this.CurrentCell = this.Rows[this.CurrentCell.RowIndex + 1].Cells[col];
}
}

}


// indicate that we have handled the keystroke
return true;

}

return base.ProcessDialogKey(keyData);

}
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
// ERROR: Not supported in C#‎: OnErrorStatement

// the process in this method is the same as that in the ProcessDialogKey method

if (e.KeyData == Keys.Enter) {
SendKeys.Send("{Tab}");

return true;
}

if (e.KeyData == Keys.Tab) {
int col = this.CurrentCell.ColumnIndex + 1;
while (col < this.Columns.Count) {
if (!(this.Columns[col].ReadOnly)) {
break; // TODO: might not be correct. Was : Exit Do
}
col += 1;
}

if (col < this.Columns.Count) {
this.CurrentCell = this.Rows[this.CurrentCell.RowIndex].Cells[col];
} else {
if (this.CurrentCell.RowIndex != this.Rows.Count - 1) {
for (col = 0; col <= this.CurrentCell.ColumnIndex; col++) {
if (!(this.Columns[col].ReadOnly)) {
break; // TODO: might not be correct. Was : Exit For
}
}
if (col <= this.CurrentCell.ColumnIndex) {
this.CurrentCell = this.Rows[this.CurrentCell.RowIndex + 1].Cells[col];
}
}
}

// indicate that we have handled the keystroke
return true;
}

return base.ProcessDataGridViewKey(e);
}
private int WM_LBUTTONDOWN = 0x201;
private int WM_LBUTTONDBLCLK = 0x203;
private int MK_LBUTTON = 0x1;
protected override void WndProc(ref Message m)
{
// ERROR: Not supported in C#‎: OnErrorStatement


if (m.Msg == WM_LBUTTONDOWN || m.Msg == WM_LBUTTONDBLCLK) {
if (m.WParam.ToInt32() == MK_LBUTTON) {
int lparam = m.LParam.ToInt32();
int xpos = lparam & 0xffff;
int ypos = lparam >> 16;

// prevent the user from selecting the readonly cell by clicking on it
if (!(IsReadonlyCell(xpos, ypos))) {
base.WndProc(ref m);
}
}
} else {
base.WndProc(ref m);
}

}
private bool IsReadonlyCell(int xpos, int ypos)
{
int column = 0;
while (column < this.ColumnCount) {
if (this.GetColumnDisplayRectangle(column, true).Contains(xpos, ypos)) {
break; // TODO: might not be correct. Was : Exit Do
}
column += 1;
}

if (column < this.ColumnCount) {
if (this.Columns[column].ReadOnly) {
return true;
} else {
return false;
}
} else {
return true;
}
}

}

vB.N3T
جمعه 24 خرداد 1392, 14:14 عصر
کدام اینه

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ClassLibrary4
{
public class Class1
{
public void d()
{
DataGridView dg = new DataGridView();

dg.Location = new Point(0, 0);
dg.Height = 200;
dg.Width = 400;

this.Controls.Add(dg);
}
}
}

Controls رو نمیشناسه

ali_md110
جمعه 24 خرداد 1392, 14:31 عصر
توجه کنید دوست من کلاس شما از کلاس فرم ارث بری کنه
وگرنه دسترسی به کالکشن Controls ندارید

class Class1:Form
در ضمن این روش روش مناسبی برای ساختن یک کنترل نیست از اون روشی که پست قبل ارسال شد استفاده کنید

vB.N3T
جمعه 24 خرداد 1392, 14:42 عصر
متوجه نشدم میشه بگید انو باید کجای فرم بزارم

khokhan
جمعه 24 خرداد 1392, 15:44 عصر
متوجه نشدم میشه بگید انو باید کجای فرم بزارم
کلاس دوست خوبمون ali_md110 در قالب یه نمونه وتوی فرم هم گرید رو با استفاده از اون کلاس ایجاد کردم

ali_md110
جمعه 24 خرداد 1392, 15:48 عصر
کلاسی که گذاشتم تو برنامتون پیاده سازی کنید
ابتدا یک کلاس یسازید تموم کدهاشو پاک کنید و یک کپی از کدهای منو توی کلاس بذارید
بعد برنامه رو Run کنید اگر به Toolbax ویژال استودیو نگاه کنید یک شکل چرخ دنده اضافه شده بنام MyGridview خب همین رو بکشید روی فرم یا یک کانتینر مثل GroupBox

vB.N3T
جمعه 24 خرداد 1392, 16:17 عصر
بله درسته الان سیو کردم و تو برنامه های خودم ازش استفاده میکنم
اما اون کدی که بالا نوشتم رو باید تکمیل کنم.مد نظر طرف اونه..میشه کمک کنید اون رو تکمیل کنم..
کلاسی که خودو نوشتم مشکلش کجاست؟؟

vB.N3T
جمعه 24 خرداد 1392, 16:25 عصر
کلاس ساخت دیتا گرید اینه درسته


public class MyGridview : DataGridView

من اینو نوشتم

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
public class MyGridview : DataGridView
{

}



و توی فرمم اینو

private void Form1_Load(object sender, EventArgs e)
{
MyGridview grid = new MyGridview();
this.Controls.Add(grid);
DataGridViewTextBoxColumn coll = new DataGridViewTextBoxColumn();
grid.Columns.Add(coll);
grid.Columns[0].HeaderText = "میردانه";
InitializeComponent();

یه دیتا اضافه کرد

vB.N3T
جمعه 24 خرداد 1392, 16:30 عصر
دوستان عزیزم ممنون مطلبو گرفتم