C#: Data Grid View Advanced Operations - c#

I am developing a windows form application in C#. I have a datagridview on windows form.
This datagridview is poupulated dynamically, through some text file.
Now what i want to achieve the following functionaity
1) Normally, when user clicks on my datagridview then the respected cell is selceted i want to remove this functionality.i.e if user click anywhere on my datagridview then noting will happens
2) Along with this datagridview i also have a menu strip in my form, the second thing which i want to achieve is that when user click on a specific button (say B) then my mouse cursor changes to plus shape(+) and it allows user to draw a box on my datagridview. On the basis of which i later perform some calculation.
Since i am new to .net thats why i don't know how to acheive this. So reference to some tutorial will also work.

You first part of the question can be done by
dataGridView1.Enabled = false;
dataGridView1.CurrentCell = null;
Can you elaborate a bit more on the second

For the 1 problem you have said, write the following line of code in Selected index changed event grid.Selectedindex=-1;

Related

c # winform mysql data display

through C # I am creating a program that manages ambulances, I am trying to create a list with the events and active missions, this data must be taken from a mysql database, I have tried to see many "tutorials", but none explained what I needed, how do you think I can do something like this?
image here
in addition to displaying the data, if I click twice it should open the event or mission to me depending on where I click, (the icons are also buttons that should only be visible if there is a data)
just a tip! I'm not asking you to write the code for me!!!
EDIT:
I managed to get the data from mysql, that you know is it possible to place the data in specific columns?
Program
Code
It is difficult to guide you through such unspecific question, but I'll try some hints.
You said you have data from MySQL
Fill a DataTable (another way is to use BindingSource)
Assign DataTable as a DataSource of your DataGridView grid.
You can edit columns in the DataGridView using UI in VS, or you can do it programatically. First is easier, just note that if you at any stage provide Nothing as a data source, you'll loose column headers.
For event fired on DataGridViewRow, click on the grid in Designer, click on events in the Properties pane, find DatagridviewRow_DoubleClick and double click the line, to create associated method
FIll in the code into the even method. Use DataGridView.SelectedRows(0) to get selected row and it's index. If you want to "open it" create a modal dialog or what you want for displaying the content.

How to enable editing of an entire DataGridViewRow?

I am trying to put an entire DataGridViewRow into edit mode after a button click event. When I need to programmatically edit a cell I use the code below, but BeginEdit doesn't work with rows, it only allows one cell to be edited.
DataGridView.CurrentCell.ReadOnly = false;
DataGridView.BeginEdit(true);
How can I make an entire DataGridViewRow editable?
There is a standard way that the DataGridView is supposed to work ... try to make it work differently and you are in for much frustration and large quantities of code.
There are several things I can think of here:
1) Find a third-party grid control that does what you need.
2) Write your own grid control to do this.
3) Try to make the DataGridView do what it was not designed to do.
I am assuming by your message that you are restricted to option 3? If so, then you could try this:
Keep a flag denoting that you are in edit mode (I hate flag-based programming - but sometimes there is no other easy choice...).
When the user clicks the button, set the flag and execute BeginEdit on the cell.
On the EndEdit, use BeginEdit to start edit of the next cell (assuming the flag is still set).
When the user presses the Enter key, exit edit on the currenlty edited cell and turn the flag off (which should prevent any other cell from being edited).

Edit and delete rows in gridview

We know asp.net gridview can edit/delete by clicking the button along with the gridview bind with a database.
Can we do it in windows form? I mean how to customize the button event or borrow asp.net framework to winform?
Please see the image.
As far as I know the winform grid do not have these abilities, You'll have to create them explicitly, like add another column and implement his onlclick/doulbe click, it really have tons of events.
p.s. if u want the user to change values on the rows displayed you'll need this:
dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystroke

Winform DataGrid with detail, what is the best way

I'm pretty new to c# and Winforms and I'm wondering what is the best approach to the following screen design.
I have a window that contain a Datagrid wich would be read-only. Beneath the grid, I have the detail of the records in differents fields (textbox, combobox, checkbox).
What I want is that when the user click on an item in the datagrid, the data will be shown in the detail fields.
That part is pretty easy, but I want to be able to update the fields automatically, wich means, I would prefer to not have to press a Save button.
Let's say that I click an item in the datagrid, change some value in the detail fields and the I click on another item in the datagrid, then I also want to perform some validation and calculation before the record get updated.
What I was thinking at first was to get the button for "new", "edit", "save" action and lock and unlock the fields accordingly and keep a flag to know if i need to insert or update the data, but then I realized that I would prefer to not have thoses button and have the save performed automatically.
Is there any sample somewhere that does what I want?
Also, would you guys using the built-in databinding functunality or just use a dataset object in code?
Pretty common scenario.
On selected row change of grid you know which datarow you shoul bind to the other controls. when same event happens again you validate and save or cancel in case of errors.
You can make use of DataGridView.CellEndEdit Event to get the new value and DataGridView.CellBeginEdit event to get the old value and update your data if there is any change

Web application Listview question

In a list view in C# how can I make all the fields in a column editable at once? Right now I have the standard edit button which allows me to update just one line at a time. I want the end user to be able to click a button and allow then to edit the column in its entirety.
Rebind the ListView with all TextBoxes when the user clicks edit.
You would have to manually track if you're in edit mode or not and on postback loop through all the rows retrieving the values from the textboxes and store them.
There is no built-in functionallity for what you're trying to do but it's possible.

Categories