0
I’m doing a C# application in Visual Studio Windows Forms without using a database, I’m controlling everything for a class (Person) and two more classes (Personal and Personal) inherited from the base class.
I’m trying to carry the DataGrid
with the data of a registration I do in the program at the moment I click on the selected line, but I’m not getting (what initially needs to appear in the lines of the DataGrid
is the first data registered and then when you select it with a click there appears all the data back in TextBox
where they were inserted). For example, I show the name of each registered user on the datagrid lines and when I click on a line, I want the recorded data to be shown back in each textbox where it was inserted.
I’m saving the registration data on a list, this way
List<Pessoa> listcadastro = new List<Pessoa>();
listcadastro.Add(new Pessoa(/*aqui eu coloco cada text box que contem os dados que eu quero*/));
but I can’t figure out how to do that and I also saw that the CellClick
. I am not posting the code because there is no error because I am not sure how to implement the code this function Rowselectionchanged to display the data registered by the textbox at the moment I click on the selected line.
I saw you have on the date Event here on this site: Rowselectionchanged Event (Webhierarchicaldatagrid) saying that there is Currentselectedrows and Previousselectedrows and would like to know if one of these really meets what I need.
Updating:
You are giving error in the save part, it is in this part to get the index
List<Pessoa> listcadastro = new List<Pessoa>();
//exibir todos os nomes que foram cadastrados um em baixo do outro na grid
int Row;
private void bt_salvar_Click_1(object sender, EventArgs e)
{
listcadastro.Add(new Pessoa(txt_nome.Text, txt_endereco.Text, int.Parse(txt_ano.Text), txt_telefone.Text));
Row = dataGridView1.CurrentRow.Index;
dataGridView1.Rows.Add(listcadastro[Row].Nome);
}
//mostrar todos os dados de volta na textbox em que foi inserido quando selecionar a linha
public void DataGridView1_CellClick(object sender, EventArgs e)
{
if (dataGridView1.RowCount > 0)
{
txt_nome.Text = dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString();
txt_endereco.Text = dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString();
txt_ano.Text = dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString();
txt_telefone.Text = dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString();
}
}
Here below I made an assembly showing how it is to stay, there on the grid appears only the name of the registered user
Please be more specific. We have no way of knowing what problem you are having if you don’t put the code you are using, tell what the expected result is and what result is being produced so far. Also, don’t use the tag
visual-studio
if your problem is not directly related to the IDE. You can see more details about IDE, compilers and programming languages in this question here of the Stack Overflow in Portuguese.– Jéf Bueno
The error in saving may be caused by
dataGridView1.CurrentRow.Index
, since you don’t necessarily have a line selected when the user clicks on the save button. The question is very confusing. I thought the problem was about how to rescue thePessoa
selected, but now it seems like saving to the list. What is the question?– igventurelli
It’s just that I’ve been updating the question. But in this case the selected person was also a question and you answered (very well, great solution) but also need to know how to get the index that is represented by x here -> dataGridView1.Rows.Add(listcadastro[x].name) (the registration data is already being saved in an array list that I put there in the code). In short, in datagrid I just want to show the name of each registered user.
– user67662