Adding DataGridView columns programmatically - c#

I have a DataGridView filled with productinformation. The datagridview has totally 50 columns but the users don't always need all the columns, I want to help them to be able to choose which columns to show and which ones not to show.
One solution that I would like to programm is that when the user right clicks on the columns they can choose from a list that pops up choose which columns to show and which ones not to shos. Just like the image below.
How can I do that. I would really appreciate any help.

You can achieve this using the WinForms ContextMenuStrip and the Visible property of DataGridView columns.
Here is some example code that does what you want:
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BindingList<User> users = new BindingList<User>{
new User{Name = "John", Address="Home Street", Title="Mr."},
new User{Name = "Sally", Address="Home Street", Title="Mrs."}
};
contextMenuStrip1.AutoClose = true;
contextMenuStrip1.Closing += new ToolStripDropDownClosingEventHandler(contextMenuStrip1_Closing);
dataGridView1.DataSource = users;
dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
}
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewColumn gridViewColumn in this.dataGridView1.Columns)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Name = gridViewColumn.Name;
item.Text = gridViewColumn.Name;
item.Checked = true;
item.CheckOnClick = true;
item.CheckedChanged += new EventHandler(item_CheckedChanged);
contextMenuStrip1.Items.Add(item);
}
foreach (DataGridViewColumn gridViewColumn in this.dataGridView1.Columns)
{
gridViewColumn.HeaderCell.ContextMenuStrip = contextMenuStrip1;
}
}
void item_CheckedChanged(object sender, EventArgs e)
{
ToolStripMenuItem item = sender as ToolStripMenuItem;
if (item != null)
{
dataGridView1.Columns[item.Name].Visible = item.Checked;
}
}
void contextMenuStrip1_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
{
e.Cancel = true;
}
}
}
public class User
{
public string Name { get; set; }
public string Address { get; set; }
public string Title { get; set; }
}
}
The User class there is just so the example compiles, providing something to bind my DataGridView to.
I've also added some code that allows users to click more than one column at a time (by checking the close reason on closing and cancelling if it was an item select). This is actually a little borderline in terms of diverting from standard UI behaviour in my opinion - it is usually better to stick with standard behaviour, but I included since it is (I think) useful in this scenario.
Also, it is generally tidier to put this sort of customisation into a new control that inherits from DataGridView.

Related

Datagridview bind Comboboxcolumns to changing list

I am trying to use a datagridview as userinput in a windows forms project. The user should be able to add new comboboxcolumns and also edit existing columns (changing, adding, deleting items).
For now I have a List of "Features"
public class Feature
{
public Guid featureID { get; set; }
public String name { get; set; }
public List<Choice> choices { get; set; }
}
public class Choice
{
public String choiceID { get; set; }
public String name { get; set; }
}
The user is able to create new features and edit existing ones. The features are then added to a list.
For every List Item there should be a comboboxcolumn in a datagridview.
When the user adds a new Feature I create the Column with the following Code:
private void addColumnToTable(Feature ft)
{
String colName = "column" + ft.name;
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.HeaderText = ft.name;
col.Name = colName;
col.DataSource = ft.choices;
col.ValueMember = "choiceID";
col.DisplayMember = "name";
dataGridView1.Columns.Add(col);
}
This works perfectly.
The user is now able to add input to the databoxcolumn.
Lets say the user selects an item in one column and later decides to delete or update the selected choice. Then I get an error, because the selected item doesn't exist anymore.
I don't want to delete the changed column and add it again, because I want the selection of unchanged Choices to be the same before and after the Feature edit.
How can I update existing columns? Is it possible to databind my featurelist to the datagridview to achieve the desired behaviour?
Thank you
In general, whenever you modify your ComboBox list options you should:
Nullify your Column.DataSource to invalidate the UI.
Set your Column.DataSource to your modified list.
Reset the ValueMember and DisplayMember - otherwise it'll bind to the whole object and not properties of the object.
currentColumn.DataSource = null;
currentColumn.DataSource = modifiedChoices; // List<Choice>
currentColumn.ValueMember = "choiceID";
currentColumn.DisplayMember = "name";
Now you have options - both of which are to nullify the invalid value(s):
Loop through the cells of that column.
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
if (row.Index != this.dataGridView1.NewRowIndex)
{
string value = row.Cells[col.Name].Value.ToString();
if (!modifiedChoices.Any(item => item.choiceID == value))
{
row.Cells[col.Name].Value = null;
}
}
}
Handle the DataGridView.DataError event as the error dialogue suggests.
this.dataGridView1.DataError += DataGridView1_DataError;
private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn)
{
this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = null;
e.Cancel = true;
}
}

How to redraw DataGridView from another form

I have a list of items that have a name, price and quantity value.
This list is stored in one form, and this form also had an edit button, so that when a user clicks on a row, they are able to edit this item inside another form that pops up.
I have my code working so that the item changes in the list, however it seems like the DataGridView just isn't updating when the list is changed.
When I edit an item, and add in a new row, it shows the changed values.
Here is my code for my first form:
private void EditButton_Click(object sender, EventArgs e)
{
EditForm editForm = new EditForm();
if (BasketGrid.RowCount > 0)
{
editForm.Show();
}
}
So this juts sets up the button so that it shows the other form.
"BasketGrid" is my DataGridView, that is also given a public initialization at the beginning of my code (Called dgv)
public void EditOkBut_Click(object sender, EventArgs e)
{
this.newName = editNameBox.Text;
decimal price;
int quant;
if (decimal.TryParse(editPriceBox.Text, out price))
{
this.newPrice = price;
}
else
{
MessageBox.Show("Incorrect format for price");
}
if(int.TryParse(editQuantBox.Text, out quant))
{
this.newQuantity = quant;
}
else
{
MessageBox.Show("Incorrect format for quantity");
}
foreach (OrderItem o in basketForm.GetList().ToList())
{
string listName = basketForm.getListName();
if (listName == o.ProductName)
{
o.ProductName = this.newName;
o.ProductPrice = this.newPrice;
o.ProductQuantity = this.newQuantity;
}
}
this.Close();
}
This is my "Edit Button" in my secondary form. This grabs my itemlist from my other form via a method, and compares the product name in of the orderitem in the list, and the listname that the user has selected from the row.
I'd created 'basketForm' as a new object of my other form, so I can access methods and stuff.
I've tried to use basketForm.dgv.Refresh(); but to no avail.
Any help is appreciated.
Cheers,
Daniel
You can use BindingSource and ShowDialog...
Example:
public partial class MainForm : Form
{
private BindingSource bindingSource = new BindingSource();
List<YourData> yourData = new List<YourData>();
public MainForm()
{
InitializeComponent();
bindingSource.DataSource = yourData;
dgv.DataSource = bindingSource;
}
}
Changes will be reflected to your grid like this...
private void EditButton_Click(object sender, EventArgs e)
{
EditForm editForm = new EditForm(yourData);
if (BasketGrid.RowCount > 0)
{
editForm.ShowDialog(this);
bindingSource.ResetBindings(true);
}
}
//Change your Data in EditForm whatever you want
public partial class EditForm : Form
{
List<YourData> yourData;
public EditForm(List<YourData> yourData)
{
InitializeComponent();
this.yourData = yourData;
}
}
You should implement INotifyPropertyChanged interface in the OrderItem class. This will update only one value in DataGridView, instead of updating the entire collection, which may be critical if the collection is very large and its binding may trigger actions, like validation, etc.
class OrderItem : INotifyPropertyChanged
{
private string name;
// other fields : price, quantity
public string Name
{
get { return name; }
set
{
if (value != name)
{
name = value;
NotifyPropertyChanged();
}
}
}
// other properties: Price, Quantity
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Also you have to use BindingList class instead of List. It supports two-way data-binding mechanism.

Make auto generated column readonly in DataGridView

I have a DataGridView whose DataSource is a DataTable with five columns. If I attempt to access a column's ReadOnly property, like so:
datagridview.Columns[1].ReadOnly = true;
It throws a NullReferenceExcpetion.
I understand this is due to how the framework manages its auto generated columns, as noted by the answer to this question.
My question is: How do I make a column(s) readonly when the data source is auto generated?
Can't really say why it's not working, but a simple test with this code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = GenerateData();
dataGridView1.Columns[0].ReadOnly = true;
}
private List<DataSourceTest> GenerateData()
{
return new List<DataSourceTest>()
{
new DataSourceTest(1, "A"),
new DataSourceTest(2, "B"),
new DataSourceTest(3, "C"),
new DataSourceTest(4, "D"),
new DataSourceTest(5, "E"),
new DataSourceTest(6, "F"),
};
}
}
public class DataSourceTest
{
public DataSourceTest(int id, string name) { ID = id; Name = name; }
public int ID { get; set; }
public string Name { get; set; }
}
and making the gridview EditMode set to EditOnEnter so we can easily check if it's readonly or not, shows that it does the job well.
But if you still have issues, the best bet is to use an event, and the closest event for your question is the DataBindingComplete that will fire after the binding is done, so on that time, you will have full access to all your columns as they already bind to the gridview object.
double click on the event in the GridView control and add your readonly setter:
private void dataGridView1_DataBindingComplete(
object sender, DataGridViewBindingCompleteEventArgs e)
{
dataGridView1.Columns[0].ReadOnly = true;
}
In true TEK fashion, I figured out a solution to my own question:
To do this, you need to make use of the ColumnAdded event
datagridview.ColumnAdded += dataGridView_ColumnAdded;
Then in the event, you can check a column by name:
private void dataGridView_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
if (e.Column is DataGridViewColumn)
{
DataGridViewColumn column = e.Column as DataGridViewColumn;
column.ReadOnly = true;
if (column.Name == "first_name")
{
column.ReadOnly = false;
}
}
}
Make column read-only when column has been generated
private void Form1_Load(object sender, EventArgs e)
{
List<Student> allStudent = new List<Student>();
for (int i = 0; i < 10; i++)
{
allStudent.Add(new Student { Name = "Student" + i, Roll = i + 1 });
}
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = allStudent;
//Edited to show column count
MessageBox.Show("Column count is " + dataGridView1.Columns.Count);
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
column.ReadOnly = true;
}
}
public partial class Student
{
public string Name { get; set; }
public int Roll { get; set; }
}

Creating Dynamic Tables in ASP.NET via button onclick event

Here are my requirements:
I have a dropdown list and text box (appName and profile).
I want to take the values from the dropdown and text box and add them to a table (or a control like gridview that renders into a
table)
At some point I want to be able to loop through the table and submit the values to a db.
My problem:
The postback caused by the onClick even is casing the table to only show the last value entered, and doesn't retain any of the previous
values.
Notes:
I tried to work arond this using a datalist bound to a datagrid, but no luck.
Code:
protected void addAppButton_Click(object sender, EventArgs e)
{
DropDownList appList = (DropDownList)newEntryFV.FindControl("appList");
TextBox profileTextBox = (TextBox)newEntryFV.FindControl("profileTextBox");
addAppsToTable(appList.SelectedValue.ToString(), profileTextBox.Text.ToString());
}
private void addAppsToTable(string appName, string profileName)
{
Table appsTable = (Table)newEntryFV.FindControl("appTable");
TableRow newRow = new TableRow();
TableCell appNameCell = new TableCell();
TableCell profileCell = new TableCell();
appNameCell.Text = appName;
profileCell.Text = profileName;
newRow.Cells.Add(appNameCell);
newRow.Cells.Add(profileCell);
appsTable.Rows.Add(newRow);
}
Code that solved my problem:
[Serializable]
public class securityApps
{
public string secAppID { get; set; }
public string secAppName { get; set; }
public string secProfile { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
BindApps();
}
protected void addAppButton_Click(object sender, EventArgs e)
{
DropDownList appList = (DropDownList)newEntryFV.FindControl("appList");
TextBox profileTextBox = (TextBox)newEntryFV.FindControl("profileTextBox");
addAppsToListVS(appList.SelectedValue.ToString(), appList.SelectedItem.Text.ToString(), profileTextBox.Text.ToString());
BindApps();
}
private void addAppsToListVS(string appID, string appName, string profile)
{
securityApps secApp = new securityApps();
secApp.secAppID = appID;
secApp.secAppName = appName;
secApp.secProfile = profile;
((List<securityApps>)ViewState["appsListVS"]).Add(secApp);
}
// Binds apps to Grid View
private void BindApps()
{
GridView appsListGV = (GridView)newEntryFV.FindControl("appsListGV");
if (ViewState["appsListVS"] != null)
{
appsListGV.DataSource = (List<securityApps>)ViewState["appsListVS"];
appsListGV.DataBind();
}
else
{
List<securityApps> appsListVS = new List<securityApps>();
ViewState["appsListVS"] = appsListVS;
}
}
How about storing a List of objects (they could even be simple key value pairs) in the ViewState. You can use that data as the DataSource for a GridView. I think that's the simplest way to go. If you need more details, let me know.
Edits-- Your solution above looks good-- I might just make it a little easier by setting up a property for your ViewState values..
List<securityApps> AppsListVS{
get
{
if(ViewState["AppListVS"] == null
this.AppListVS = new List(securityApps)();
return (List<securityApps>)ViewState["AppListVS"];
}
set
{
ViewState["AppListVS"] = value;
}
}

datagridView edits

I have a datagridview that is bound to a SQLite database. Now, I wish to modify some fields in the datagridview.
There are 4 TEXT columns - Timestamp, Message, Type , Hash.
Now I find a row, I want to right click it.. And it should have to option - "include" .. So, when i click include in the context menu, the Type column of my DGV should change to "include" from whatever it previously was... (I don't want it be enabled to edit.. I just want it to change within the program) How do i get the index where I have clicked and access that particular cell to modify it??
This code does what you want:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BindingList<User> users = new BindingList<User>();
users.Add(new User(){Name = "Fred", Included = "False", Title="Mr"});
users.Add(new User(){Name = "Sue", Included = "False", Title="Dr"});
users.Add(new User(){Name = "Jack", Included = "False", Title="Mr"});
dataGridView1.DataSource = users;
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
if (hit.rowIndex >= 0)
{
dataGridView1.ClearSelection();
dataGridView1.Rows[hit.RowIndex].Selected = true;
contextMenuStrip1.Show(this.dataGridView1, new Point(e.X, e.Y));
}
}
}
private void includeToolStripMenuItem_Click_1(object sender, EventArgs e)
{
// Included was the name of the column to change in my example code,
// you could also use the index of the column if you know it.
dataGridView1.SelectedRows[0].Cells["Included"].Value = "Included";
}
}
public class User
{
public string Name { get; set; }
public string Title { get; set; }
public string Included { get; set; }
}
I couldn't think of a better method of informing the context menu which row was selected than actually using the selected row property of the DataGridView - you could also store this in a class level field, but I don't think that is quite as tidy.
private void dataGridView_DoubleClick(object sender, EventArgs e)
{
var grid = (DataGridView)sender;
var point = grid.PointToClient(Cursor.Position);
var hit = grid.HitTest(p.X, p.Y);
MessageBox.Show(string.Format("{0} / {1}", hit.ColumnIndex, hit.RowIndex));
}
Code is not compile tested, but in theory this should do the job.
dataGridView.Rows[rowIndex].Cells[cellIndex].Value = "something";

Categories

Resources