PDA

View Full Version : دستور بالا و پایین کردن سطرهای Datagridview



pcb20parse
دوشنبه 22 دی 1393, 17:06 عصر
دوستان با چه کد میشه سطرهای datagridview جا به جا کرد کرد؟ یعنی سطر بالا رو بیارم پایین و سطر پایین رو ببرم بالا؟
و اینکه من از دیتا گریدویو برای درج اطلاعات اطلاعات فاکتور استفاده کردم. یک ستون رو به نام ردیف کالا گذاشتم میخواستم بدونم آیا قابلیت این رو داره که بشه این ستون رو طوری تنظیم کنم که با افزایش کالا به کالا رقم ردیف اضافه بشه و با کم کردن کالا از رقم کالا کم بشه؟

sajadsobh
دوشنبه 22 دی 1393, 22:52 عصر
اول دو تا تابع واسه بالا و پایین کردن سطر انتخابی تعریف شده بعدشم دو تا دکمه به اسم up و down در نظر گرفته شده که بشه سطر انتخابی رو به بالا و پایین انتقال داد:

private void moveUp()
{
if (dataGridView1.RowCount > 0)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int rowCount = dataGridView1.Rows.Count;
int index = dataGridView1.SelectedCells[0].OwningRow.Index;

if (index == 0)
{
return;
}
DataGridViewRowCollection rows = dataGridView1.Rows;

// remove the previous row and add it behind the selected row.
DataGridViewRow prevRow = rows[index - 1];
rows.Remove(prevRow);
prevRow.Frozen = false;
rows.Insert(index, prevRow);
dataGridView1.ClearSelection();
dataGridView1.Rows[index - 1].Selected = true;
}
}
}

private void moveDown()
{
if (dataGridView1.RowCount > 0)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int rowCount = dataGridView1.Rows.Count;
int index = dataGridView1.SelectedCells[0].OwningRow.Index;

if (index == (rowCount - 2)) // include the header row
{
return;
}
DataGridViewRowCollection rows = dataGridView1.Rows;

// remove the next row and add it in front of the selected row.
DataGridViewRow nextRow = rows[index + 1];
rows.Remove(nextRow);
nextRow.Frozen = false;
rows.Insert(index, nextRow);
dataGridView1.ClearSelection();
dataGridView1.Rows[index + 1].Selected = true;
}
}
}

private void up_Click(object sender, EventArgs e)
{
moveUp();
}

private void down_Click(object sender, EventArgs e)
{
moveDown();
}

sajadsobh
دوشنبه 22 دی 1393, 23:42 عصر
واسه ردیف کالا هم می تونید از row header استفاده کنید. توی رویداد RowsAdded از گریدویو این قطعه کد رو استفاده کنید:

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString();
}

pcb20parse
شنبه 11 بهمن 1393, 14:33 عصر
ممنونم اما مشکل اصلی من اینه که سطرها به جدول bound شده و امکان جا به جایی نیست

sajadsobh
شنبه 11 بهمن 1393, 15:38 عصر
آها. من فکر کردم گریدویو خام هستش.
برای اینکاری که مد نظرتونه باید ترتیب جدول رو بتونین بهم بزنید. به نظر من منطقیش اینه که شما یه فیلد به اسم مثلاً "الویت" قرار بدین. بعد جدول رو به براساس اون مرتب کنین. یعنی وقتی که میخواین یه سطر جابجا بشه باید مقدار اون فیلد کم یا زیاد بشه. اینجوری می تونید اون سطر رو حرکت بدین. یکم فکر کنین اگه به جواب نرسیدین من کمک میکنم.

hamid30sharp
یک شنبه 12 بهمن 1393, 00:41 صبح
اول دو تا تابع واسه بالا و پایین کردن سطر انتخابی تعریف شده بعدشم دو تا دکمه به اسم up و down در نظر گرفته شده که بشه سطر انتخابی رو به بالا و پایین انتقال داد:

private void moveUp()
{
if (dataGridView1.RowCount > 0)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int rowCount = dataGridView1.Rows.Count;
int index = dataGridView1.SelectedCells[0].OwningRow.Index;

if (index == 0)
{
return;
}
DataGridViewRowCollection rows = dataGridView1.Rows;

// remove the previous row and add it behind the selected row.
DataGridViewRow prevRow = rows[index - 1];
rows.Remove(prevRow);
prevRow.Frozen = false;
rows.Insert(index, prevRow);
dataGridView1.ClearSelection();
dataGridView1.Rows[index - 1].Selected = true;
}
}
}

private void moveDown()
{
if (dataGridView1.RowCount > 0)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int rowCount = dataGridView1.Rows.Count;
int index = dataGridView1.SelectedCells[0].OwningRow.Index;

if (index == (rowCount - 2)) // include the header row
{
return;
}
DataGridViewRowCollection rows = dataGridView1.Rows;

// remove the next row and add it in front of the selected row.
DataGridViewRow nextRow = rows[index + 1];
rows.Remove(nextRow);
nextRow.Frozen = false;
rows.Insert(index, nextRow);
dataGridView1.ClearSelection();
dataGridView1.Rows[index + 1].Selected = true;
}
}
}

private void up_Click(object sender, EventArgs e)
{
moveUp();
}

private void down_Click(object sender, EventArgs e)
{
moveDown();
}


من نتونستم استفاده کنم میشه بگید در کدوم اونت باید اضافه بشه

sajadsobh
یک شنبه 12 بهمن 1393, 14:57 عصر
توی event نباید بنویسین. دو تا تابع تعریف شده. با کلیک روی دکمه ها این توابع فراخوانی میشه. یه دکمه به اسم up و یه دکمه به اسم down گذاشتم و این توابع رو توش صدا زدم. همین.