Srihari
Srihari

Reputation: 2429

How to add unbound column to gridview and delete button on runtime?

I have gridview I can able to fill data's in runtime. How can i add unbound columns to that gridview in runtime. After that I need to add a delete button and lookupedit on that unbound columns. Finally How to write event on that particular lookupedit and delete buttons ?

Upvotes: 0

Views: 1846

Answers (2)

Farhad Alizadeh Noori
Farhad Alizadeh Noori

Reputation: 2306

Here is an example of creating a column for a datagridview in runtime:

grdExpressions.SuspendLayout();
grdExpressions.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

DataGridViewCheckBoxColumn enabledColumn = new DataGridViewCheckBoxColumn();
enabledColumn.Name = "columnEnabled";
enabledColumn.HeaderText = "EN";
enabledColumn.FillWeight = 1;
enabledColumn.MinimumWidth = 30;
grdExpressions.Columns.Add(enabledColumn);

grdExpressions.ResumeLayout(false);
grdExpressions.PerformLayout();

You basically create a DataGridView(CheckBox|TextBox|etc)Column first. Whichever type of column you want. If by lookupedit you mean a textbox and button in the same column, there is no such column in the standard datagridview. You can however add a ButtonCell and a text before it. There are also user controls out there that do that.

In order to write events on the checkbox column in the code I provided for example, you need to handle the CellClick of the datagridview. Or if you want to handle the button click of an added ButtonCell, again you handle the CellClick and check which column was clicked and act accordingly.

In order to handle a TextChanged of a text box column, you handle the CellEndEdit of the datagridview. Basically you have to interact with all these different types of events, using the datagridview's events and then detecting the column the click occured in using the event args passed and act accordingly.

Upvotes: 1

DotN3TDev
DotN3TDev

Reputation: 138

you can add columns to a datagridview by:

dataGridView.Columns.Add(new DataGridViewColumnType(), "SomeName");

in order to add a delete button in run time you just need to have a button on the form which is disabled at the form load, you disable it at load with this code:

this.btnDelete.Enabled = false;

to enable it in runtime you do:

this.btnDelete.Enabled = true;

you would do the same for the look up edit. To add events on these you just would have the events coded prior to running the program and they would be called when things happen.

Let me know if this doesn't make sense.

Upvotes: 1

Related Questions