Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, February 25, 2011 3:54 PM
Hi all
When I like to add a new Row in DataGridView I have to scroll
down to the bottom of the grid ...
I would like to be able to add the new row on top of the grid,
this makes it much easyer for the user to add data to the grid.
How can this be done ?
Thanks and best regards
Frank Uray
All replies (3)
Friday, February 25, 2011 4:45 PM ✅Answered
I don't know that I would use a datagridview for data entry as it is primarily a data container control. I would use a form for entering data and submitting it to a dataTable which you could then display the dataTable data in the datagridview. But if you want to display a new row at the top in the datagridview -- one thing you could do is to base the datagridview on a dataview control which would be based on a dataTable which contains an autoIncrement column. You can sort the dataView control on the increment column (call it rowID for example). DataView dv = new DataView(); dv.Table = yourDataTable; dv.Sort = "rowID DESC";
Friday, February 25, 2011 5:41 PM ✅Answered
Hello frank.uray, What I think about your question is, we can't do what you want. If I am right, then you first want to bound a data source to DataGridView, then add an empty row at index 0 in that DataGridView which will be containing input controls, so that user can enter detail. But this is not possible as we can't programmatically add a row to any DataGridView if that DataGridView is Data Bounded already.
But alternatively you can place required number of controls like textbox above to the DataGridView, So that user can enter detail in those controls and after User finished adding text, You can update the data source from which you are extracting the data and refresh DataGridView for the change.
Friday, February 25, 2011 6:52 PM ✅Answered
Hi,
I did some thinking on your good idea, and came to the conclusion to do the code for you. But a bit different. This code actually does a new row where ever you want to. The idea is - where you click the row, by pressing the button to add row, there a new row is added. So you can now add rows all over the dgv control. From 1st row to the last. Its an example code, so you can moderate it - expecially the custom class (DGV_Columns), and add the class you have to show the data in dgv.
What do you think?
here`s the code:
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 Feb25DGV_AddRowOnTop
{
public partial class Form1 : Form
{
BindingList<DGV_Columns> bindingList;
public Form1()
{
InitializeComponent();
CreatingDGV();
button1.Click += new EventHandler(button1_Click);
}
private void CreatingDGV()
{
//Creating dgv and stuff:
bindingList = new BindingList<DGV_Columns>();
dgv.AllowUserToAddRows = false;
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgv.DataSource = bindingList;
this.Controls.Add(dgv);
//Example code of binding source population:
DGV_Columns[] array = new DGV_Columns[] { new DGV_Columns(1, "John Someone"),
new DGV_Columns(2, "Sara Flatcher"),
new DGV_Columns(3, "George Washington") };
for (int i = 0; i < array.Length; i++)
bindingList.Add(array[i]);
}
private void button1_Click(object sender, EventArgs e)
{
int row = dgv.Rows[dgv.CurrentCell.RowIndex].Index;
//add new empty row to binding list:
List<DGV_Columns> newList = new List<DGV_Columns>(new List<DGV_Columns>(bindingList));
bindingList.Clear();
int counter = 0;
foreach (DGV_Columns item in newList)
{
if (row == counter++)
bindingList.Add(new DGV_Columns(0, ""));
bindingList.Add(item);
}
newList.Clear();
newList = null;
}
internal class DGV_Columns
{
public int id { get; set; }
public string name { get; set; }
public DGV_Columns(int _id, string _name)
{
this.id = _id;
this.name = _name;
}
}
}
}
Hope it helps a bit,
Mitja