Hey there. Troubled once again, and this one i cant find on the internet.

I have a datagridview. I can update it perfectly, when i type in data into the datagridwiew and press the button. So that works.

Now i have a form that pops up with 2 txtboxes and a date Lable. that when the button is clicked it will store those 3 values into the datagridview, update it, and exit. eccept... it doesnt want to work...
Its like if i add data to the datagridview i assume the datagridview is selected when i press the update button. But when the values are added programatically, it appears on my datagridwiew, but it doesnt want to update it as, (i think) the datagridview isnt selected or something??

Do i have to add code that selects the datagridview before it updates?

This is my code.

private void button1_Click(object sender, EventArgs e)
        {
            // insert Values into Datagridview
            
            tblAllEntries.Rows[0].Cells[1].Value = lbldate.Text.ToString();
            tblAllEntries.Rows[0].Cells[2].Value = txtboxReminder.Text.ToString();
            tblAllEntries.Rows[0].Cells[3].Value = txtboxCost.Text.ToString();

            tblAllEntries.Rows[0].Cells[0].Selected = true; ---<<< I added this because i thought it had to be selected, but it didnt work..
            
            dAdapter.Update(dTable);
            
          

        }

that is button tho insert it and update. But it only adds it to the datagridview and doesnt want to update.

If i add a button with just ' dAdapter.Update(dTable);' It updates what i Manually Tiped into the datagridview, and works.

What can i do to save it INSTEAd of putting in a method to run that button after the save button has been clicked?

Thank you =)=)

Do you want to insert a new row on the top of the dgw, or at the bottom (into a new row)?
If is the last thing, you 1st need to count all the rows and then insert the values into the allRows + 1(so into a new row):

private void button1_Click(object sender, EventArgs e)
        {
            // insert Values into Datagridview
            int allRows = tblAllEntries.Rows.Count;
            
            
            tblAllEntries.Rows[allRows].Cells[1].Value = lbldate.Text.ToString();
            tblAllEntries.Rows[allRows].Cells[2].Value = txtboxReminder.Text.ToString();
            tblAllEntries.Rows[allRows].Cells[3].Value = txtboxCost.Text.ToString();

            tblAllEntries.Rows[allRows].Cells[0].Selected = true; ---<<< I added this because i thought it had to be selected, but it didnt work..
            
            dAdapter.Update(dTable);
          } 
     }

this should do it.

If you want to insert a new row into 1st row of dgv, you will have to create a dataTable before inserteing a new row, fill it with the data from dgv, insert a new row into dgv (or into dataTable - in 1st row), and populate dgv from the dataTable.
But this doesnt make sence - for inserting new row, its best to do an insertion into last row of dgv.

Do you want to insert a new row on the top of the dgw, or at the bottom (into a new row)?
If is the last thing, you 1st need to count all the rows and then insert the values into the allRows + 1(so into a new row):

private void button1_Click(object sender, EventArgs e)
        {
            // insert Values into Datagridview
            int allRows = tblAllEntries.Rows.Count;
            
            
            tblAllEntries.Rows[allRows].Cells[1].Value = lbldate.Text.ToString();
            tblAllEntries.Rows[allRows].Cells[2].Value = txtboxReminder.Text.ToString();
            tblAllEntries.Rows[allRows].Cells[3].Value = txtboxCost.Text.ToString();

            tblAllEntries.Rows[allRows].Cells[0].Selected = true; ---<<< I added this because i thought it had to be selected, but it didnt work..
            
            dAdapter.Update(dTable);
          } 
     }

this should do it.

If you want to insert a new row into 1st row of dgv, you will have to create a dataTable before inserteing a new row, fill it with the data from dgv, insert a new row into dgv (or into dataTable - in 1st row), and populate dgv from the dataTable.
But this doesnt make sence - for inserting new row, its best to do an insertion into last row of dgv.

Hey there. I get what you are saying with inserting a new row and all, but i dont want to do that now as i FirSt want to see if the thing could just update.

So what my program is doing at this moment is it should only OVERwRite the verry firts line the whole time with the imputed data in the textboxes so i can see if the updating works. And still it doesnt.

So i get what you said, i just want to get the updating working then ill add a new line and not over write the data.

Because like i said it updates when i manually insert the data into the datagridview, but when i insert it programatically it doesnt update. I could quickly write a demo if you dont get it if you want?

guess ill just do it my way

Do you want to insert a new row on the top of the dgw, or at the bottom (into a new row)?
If is the last thing, you 1st need to count all the rows and then insert the values into the allRows + 1(so into a new row):

private void button1_Click(object sender, EventArgs e)
        {
            // insert Values into Datagridview
            int allRows = tblAllEntries.Rows.Count;
            
            
            tblAllEntries.Rows[allRows].Cells[1].Value = lbldate.Text.ToString();
            tblAllEntries.Rows[allRows].Cells[2].Value = txtboxReminder.Text.ToString();
            tblAllEntries.Rows[allRows].Cells[3].Value = txtboxCost.Text.ToString();

            tblAllEntries.Rows[allRows].Cells[0].Selected = true; ---<<< I added this because i thought it had to be selected, but it didnt work..
            
            dAdapter.Update(dTable);
          } 
     }

this should do it.

If you want to insert a new row into 1st row of dgv, you will have to create a dataTable before inserteing a new row, fill it with the data from dgv, insert a new row into dgv (or into dataTable - in 1st row), and populate dgv from the dataTable.
But this doesnt make sence - for inserting new row, its best to do an insertion into last row of dgv.

Oh yea, and its

int allRows = tblAllEntries.Rows.Count - 1 ;

otherwise its out of bounds

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.