C# Gridview, rearrange - c#

I really didnt know what to put to the title so sorry about that. I have 2 colums where i list files from 2 folders. Now what i would like to do is to give a user ability to rearrange the files by clicking on one file in one column and then on the other file in other column. The application merges these two files together.
Just onother option would be just use datagrid and its drag and drop functionality (one column is static and one rearrangable with drag and drop or something) but doing it this way isnt really what i would like..
So, all options are welcome..
EDIT:
Using WinForms, it doesnt have to be gridview, just i couldnt think of anything else..

I would just create two DataGridView's (or ListView if you prefer), the first listing the files in the folder A and the second listing the files in the folder B.
Then allow to select just one row at a time in both grids (MultiSelect = false, SelectionMode = FullRowSelect) and add a button called "Merge Selected", that simply merges the file selected in the first grid with the one selected in the second grid.

Use 2 list boxes linked to each folder so that the use can scroll up/down to select the file they need
Have button "Merge Files" which gets enabled when an item from both listboxes are selected.
When user clicks on "Merge Files" have a confirmation box to make sure its not clicked my mistake.

I'll assume you're talking about DataGridView, as DataGrid has been deprecated.
In the designer, make sure SelectionMode on the DataGridView is set to CellSelect. Then in properties --> events, double-click the SelectionChanged event to create a new method which handles that event.
Add this code to the method:
private DataGridViewCell _lastCellSelected = null;
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
if(dataGridView.SelectedCells.Count == 0)
{
_lastCellSelected = null;
return;
}
DataGridViewCell selectedCell = dataGridView.SelectedCells[0];
if(_lastCellSelected == null || selectedCell.ColumnIndex == _lastCellSelected.ColumnIndex)
{
//User clicked first cell
_lastCellSelected = selectedCell;
}
else
{
//User has clicked two cells from different columns
string filename1 = _lastCellSelected.Value;
string filename2 = selectedCell.Value;
//TODO: "Merge" files here
_lastCellSelected = null;
}
}

Related

Show the selected row record in textbox/label

How do I make the selected row record from a DataGridView show in a TextBox? I got some TextBox and Label in a Form. I want the text inside the TextBox/Label to change when the user selects a row record from the DataGridView. I tried the following code to make it happen, but it doesn't work
private void ItemTable_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
label_itemid_show.Text = ItemTable.Rows[e.RowIndex].Cells[0].Value.ToString();
text_itemname.Text = ItemTable.Rows[e.RowIndex].Cells[1].Value.ToString();
text_itemprice.Text = ItemTable.Rows[e.RowIndex].Cells[2].Value.ToString();
text_itemstock.Text = ItemTable.Rows[e.RowIndex].Cells[3].Value.ToString();
}
I'm working on a project that includes a features similar to yours. Currently I select a record in a datagridview and need to show it's values to textboxes so that any values can be edited.
This is how I tackle the problem.
Models.Item item = DAL.ItemDAL.GetItem(Convert.ToInt32(ItemDataGrid.CurrentRow.Cells[0].Value));
tbModifyItemID.Text = Convert.ToString(item.ItemID);
tbModifyItemName.Text = item.ItemName;
It's the ItemDataGridView.CurrentRow.Cells[0].Value that finds me the ItemID of the selected record.
In your case it's [DataGridName].CurrentRow.Cells[FieldNo, starting at 0].Value.ToString();
This allows you to click on any cell in the record and still retrieve the specified cell in the code.
Hope it helps c:
PS:
If you put it inside a
private void ItemDataGrid_SelectionChanged(object sender, EventArgs e)
method it will run the code anytime you click a different cell in the datagridview.
You can use the following:
lblDetails.Text = dgMainGrid.SelectedRows[0].Cells[1].Value.ToString();
lblDetails: A label on the form.
dgMainGrid: DataGrivView on the form.
PS: Just make sure you select the row inside the datagrid and not any particular cell, row selecting is done by clicking on the left of your first column i.e. the row selector column which gets added at the run time in WinForms.

Selecting entire row in DataGridView programmatically (without using ...Rows[].Selected)

I've been trying to create Mp3 list using DataGridView to show info. I want to add data at runtime and I have Columns like Artist, Song Name, Rating, Path... but I want to be able to select entire Row when selecting a Cell. I've used this code for that:
private void DataViewGrid1_MouseClick(object sender, MouseEventArgs e)
{
int rindex = DataViewGrid1.CurrentCell.RowIndex;
DataViewGrid1.Rows[rindex].Selected = true;
}
and it works but the problem is that it's to sloooow! When I click on the cell whole row is selected but it's visually terrible. I can see cell selected and after a delay whole row is selected but the delay is just too long. Is there a faster (or better) way to do this? Or maybe there is a better control that can do this? I also want to be able to present rows in a different font (for example: change font color for the same artist in the list).
I'm open for all suggestions.
Thanks!
DataViewGrid1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
The entire row will be selected by clicking its row's header or a cell contained in that row.
If you want to color based on having a value in the cell, something like this will do the trick.

ComboBox Selected Value to be Blank

Uses: VS 2012;
I've a combobox attached to a datasource in my form. And things work fine. When I run the form, again everything works fine; I can select an item in the dropdown list and it updates to the datasource as well. My problem comes when I need to deselect/revert what I have selected after I saved or Remove what I have select (basically should go as null for that field value).
Our legacy system was built in Delphi 3 & 5, and users got a feature of right-clicking on the dropdown list and get a small popup like button named
Blank
which blanks what have been selected. I could not find anything that will do the same what ever user have selected in .NET's combo box.
You can add a new item in dropdown named -Select-( or something similar name) by using following code:
drp.DataSource = dataSet;
drp.DataBind();
// do it after binding
drp.Items.Insert(0, new ListItem("-Select-", "NA"));
If you are binding in xaml then on page_load event you can write only this line
drp.Items.Insert(0, new ListItem("-Select-", "NA"));
Now if user want to deselect choice, he/she will simply select -Select- item.
Whilst thanking all of your Answers and suggestions, I used #V4Vendetta's idea and composed my solution.
Similarly to deleting a record in a datagridview where you click Delete key, I took the same concept and associated my solution with Delete Key.
What I did was created a handler for ComboBox's Keypress Event like:
private void comboBox_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
(sender as ComboBox).SelectedIndex = -1;
}
}
And linked to every ComboBox's available
ComboBox1.KeyDown += new KeyEventHandler(comboBox_KeyPress);
ComboBox2.KeyDown += new KeyEventHandler(comboBox_KeyPress);
Now when user clicks Delete key while the ComboBox selected/active, it gets blank.

Event Handler is not getting called for dynamically created checkboxes

I am kind of new to C# and Asp.Net, so this question might sound repetitive but I am not able to find a solution for this particular problem.
I have two buttons on my HTML page and a single class in .cs file. On one of the button clicks, I create a table programmatically (dynamically).
The table contains some checkboxes which are also created dynamically.
Table creation is one of the last tasks that I perform. Before that, I read several files and extract data from them to create the table. After the table is drawn, the user can select one or more checkboxes.
Now, how on second button click, can I know that which of the checkboxes were checked before the page reload? Currently I have made all these checkboxes member variables of the the only class that I have in the .cs file.
I tried adding checkbox event handler through C# code. But the handler is not getting called when the checkbox is checked. I don't want to set the 'autopostback' property of the checkbox to true since if thats set true, the page reloads after checking one of the checkboxes. User should be able to select multiple checkboxes.
Add your checkboxes dynamically and set a unique name for each checkbox. Checkboxes are only posted back to the server if the checkbox is checked, so you can test to see if it is checked by checking Request.Form to see if the name exists. For example, lets say you named your check boxes chk_[0-9] (i.e. chk_0, chk_1 etc till 9), you could check if they ticked by doing:
for(int i=0; i < 10; i++)
{
string chk_name = "chk_" + i.ToString();
if (Request.Form[chk_name] != null)
{
//checkbox is checked
}
else
{
//checkbox is not checked
}
}

Textbox control in DataGridView

I am working on a datagridview in C# in windows application. I want to add textbox controls in DataGridView. So, when we run it then textbox should be shown in gridview and we can put value in it and My grid has 3 columns and I want to add new row in grid when I press tab on 3rd column of gridview.
How do I do this?
It's hard to provide a precise answer since your question is lacking in detail and pretty general, but to get textboxes in your DataGridView, you're going to want to add some instances of DataGridViewTextBoxColumn to the DataGridView's Columns collection. This will cause them to be populated with textboxes in each row.
To detect when the user presses tab on the 3rd column, you can add a 1-2 pixel wide fourth column and detect that it has recieved focus (almost definitely from a tab keystroke) using the OnCellEnter event.
Good luck!
So, for the "display textboxes by default portion of your question, here's the skinny:
On GridView->Edit Columns, add the columns you want to use explicitly. Then click on the link "Convert this field into a templateField". This will let you tweak the generated HTML for those cells. Say OK. Then go to GridView->Edit Templates. For your favorite Column, copy the ItemEditTemplate into the ItemTemplate. (ItemTemplate is the default. ItemEditTemplate contains the properly bound edit control.) Now all of your data fields will default to "editable."
I'm guessing you have a submit button. You'll need to tell the GridView to update the rows on submit., like so:
For Each r As GridViewRow In GridView1.Rows
Dim mon = System.Int32.Parse(CType(r.FindControl("TextBox1"), TextBox).Text)
If mon <> 0 Then GridView1.UpdateRow(r.RowIndex, False)
Next
Obviously, you'll want different logic inside there, but the basic loop/findControl/updateRow logic should apply.
Microsoft has a walkthrough on this here: Performing Bulk Updates to Rows Bound to a GridView
Try this, for example if you want to set your first column in datagridview as a textbox control:
private void dtgrdview_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
TextBox txtbox1=new TextBox();
dtgrdview.Controls.Add(txtbox1);
Rectangle rectangle = dtgrdview.GetCellDisplayRectangle(0, e.RowIndex, true);
txtbox1.Location = rectangle.Location;
txtbox1.Size = rectangle.Size;
txtbox1.TextChanged += txtbox1_TextChanged;
txtbox1.Leave += txtbox1_Leave;
txtbox1.Visible = true;
}
}
and don't forget to add this event in the same class as like below to call it when the cell have the focus:
private void txtbox1_Leave(object sender, EventArgs e)
{
txtbox1.Visible = false;
}
private void txtbox1_TextChanged(object sender, EventArgs e)
{
dtgrdview.CurrentCell.Value = txtbox1.Text;
}
If you have any other questions, don't hesitate to ask me :)

Categories