using System.Windows.Forms.Datagrid - c#

Coding on Visual C# since a few days ago.
Trying to access the elements in a DataGrid using the following code
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
textBox2.Text = this.dataGridView1.SelectedCells().Value;
}
Throws the next exception
Non-invocable member 'System.Windows.Forms.DataGridView.SelectedCells'
cannot be used like a method.
What's the problem with SelectedCells then? What's the best practice?
EDIT: I guess I'll just convert the member to string and see how it goes.

Selected Cells is a property not a method.
DataGridView.SelectedCells Property
How to: Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridView Control

From Msdn: DataGridView.SelectedCells - Gets the collection of cells selected by the user.
Test to see if SelectedCell.Count = 1 (only one cell was selected) then textBox2.Text = SelectedCells(0).Value
Alternativelly try using the CurrentCell property.

Related

Get value from selected combox and put it on textbox c#

Mabuhay!
Hi! I search this code here but unfortunately didnt get what I am looking for. Any help on this?
I got this form and have a combo box which i uses dataset so I can get the value of description on this table. And I have a textbox after that but I wanted to get the value of labor cost based on what I selected on combo box.
heres for my combobox
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the '_10daliriPayrollDataSet.Description' table. You can move, or remove it, as needed.
this.descriptionTableAdapter.Fill(this._10daliriPayrollDataSet.Description);
}
for my laborcost.Text value
private void description_SelectedIndexChanged(object sender, EventArgs e)
{
laborcost.Text = description.SelectedItem.ToString();
}
I got this error;
System.Data.DataRowView
Any help on this?
Thanks,
Chris
try
description.SelectedItem.Text this will do
The correct way of retrieving the item text is the GetItemText method.
It works for any item, including the selected one. The later can be retrieved from the SelectedItem property.
In your case
laborcost.Text = description.GetItemText(description.SelectedItem);
Please note that SelectedText and SelectedValue properties (suggested by some commenters) have different semantics and cannot be used for this purpose.
Try this,
description.SelectedValue.ToString();
You need to define DataValue member and Text Member field on your combobox. Its binding with DataRowView.
So just add two line after
this.descriptionTableAdapter.Fill(this._10daliriPayrollDataSet.Description);

DataGridView: Format values without actually changing the bounded data?

I've been searching through the web but I have not been able to find an answer.
I have a DataGridView with a BindingSource bounded, which has a List of objects of a custom class. Among the fields of the class, I have a string field which I want to show using
Path.GetFileName();
because it contains the whole filepath and what I want is to show just the name of the file but keeping the filepath (i.e., the bounded data intact) in the bounded object.
It is there any way to do this (Format, template, styles, ...)? because anytime I change the Value field of the Cell, it changes the value of the object (which it's logic).
Many thanks in advance
Use the DataGridView.CellFormatting event on the DataGridView. This will give you a DataGridViewCellFormattingEventArgs object with information about the current cell, including the row and column and the unformatted value. You update the Value property to the formatted value and set FormattingApplied to true, and the DataGridView will render that value.
Something like this:
private void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 0) // Check for the column you want
{
e.Value = Path.GetFileName(e.Value.ToString());
e.FormattingApplied = true;
}
}
The Binding class has a Format() and Parse() event handler where you can define how the data will be formatted in both ways.

how to make my C# ComboBox display a value when it is running?

I just created a ComboBox in my VS2008. I put four items for it, One, Two, Three and Four. When I run it, nothing displayed by default. I need to select one to display. How can I make it displaying the second item by default at very beginning without my selection? I tried by put a number inside the DisplayMember and ValueMeme property but it doesn't work.
thanks,
Use the SelectedIndex property:
private void MyForm_Load(object sender, EventArgs e)
{
ComboBox1.SelectedIndex = 0;
}
You can set the SelectedIndex property of the combobox. Setup work like this is often done in the Form's Load event.

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 :)

Controlling C# DataGridView with an Arraylist in VS2008

I'm having some problems with a datagridview element I'm using in VS2008.
This DataGridView is actually a tab in a TabControl element.
I gave it 5 colums which need to be filled up with elements from a costum Object i made.
It's basically a small library application which contains a main class and several classed derived from it. They all have a ToString() method which represents the data as a string of keywords containing the values needed for me to fill up the datagridview.
I only need the first 5 though, some objects will have up to 12 keywords.
Currently, Whenever I add an object, the datagrid doesn't fill itself, instead it adds an amount of columns equall to the amount of keywords the specific object has.
What i'm currently doing is this:
public void libDataGrid_Click(object sender, EventArgs e)
{
if(this.manager.Lib.LibList[0] != null)
{
libDataGrid.DataSource = this.manager.Lib.LibList;
libDataGrid.Refresh();
}
}
this.manager.Lib.LibList returns and ArrayList, in which all objects are stored. The ArrayList can contain elements of all derived classes, but since they are all connected, the string representation will always contain the elements I need to fill up the grid.
I don't see how I can filter only the first five and them have them put in the correct colums.
And another thing. Currently I can only refresh the DataGridView by clicking it. It should change on when I switch to it switch to its specific tab on the Tabcontrol I mean.
I tried adding an argument for SelectedIndexChanged, but that does nothing really...
Or at least, it doesn't appear to do anything.
What I mean is I commented out the code above and added this instead:
public void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
libDataGrid.DataSource = this.manager.Lib.LibList;
libDataGrid.Refresh();
}
This refreshes it everytime the tab is changed, no matter to which one.
I had to remove the if-statement, since it gave me an Exception. Probably because the length of the ArrayList isn't set on initialisation.
I'm a little confused by the question, but here are some thoughts:
DataGridView has an AutoGenerateColumns property; if you don't want it to create its own columns, set this to false
To bind to existing columns, the DataPropertyName must be set on each
DataGridView (in cmomon with any list control using TypeDescriptor) will hugely prefer List<T> (for some T != object) to ArrayList, since it can get meta-data even for an empty list. In general, in 2.0 using ArrayList is a mistake.
I can only give a partial answer but I think the reason that
public void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
libDataGrid.DataSource = this.manager.Lib.LibList;
libDataGrid.Refresh();
}
isn't working, is because you need to add this line where tabControl1 is being initialized. I've had this problem where VS won't do this itself.
tabControl1.SelectedIndexChanged += new EventHandler(tabControl1_SelectedIndexChanged);
If I am understanding your problem, it seems similar to a problem that I was struggling with recently in this thread on DataGridViews in C#/.NET2.0
Try calling:
libDataGrid.Invalidate();
This should force Windows to redraw your control. No need to reattach the datasource and refresh. (I think you can safely comment out those 2 lines.)
Also: What was the Exception that you were getting?
And did you use the "Data Source Configuration Wizard" to help you with the dataGridView?

Categories