Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
I have a datagridview bound to a database-table. the dgv shows data of an item,
where the item could be everything.
when adding a new item the dgv's DataSource has 0 rows, the dgv just shows the columns (correctly).
to add data to the item, I use
Item_CellMouseDown
to add the items id
to the new row, because the id-column is not visible in dgv.

below a two versions of the method and none works correctly.

code A :
private void Item_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    int row = e.RowIndex;

    DataRow dr;
    if (row == SourceTable.Rows.Count) {
       dr = SourceTable.NewRow();
       SourceTable.Rows.Add(dr);

       dr["ID"] = this.ID;
    }
}

code B :
private void Item_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    int row = e.RowIndex;

    object  id = Item["Id",row].Value;
    if (id == null || id.ToString() == string.Empty) {
       Item["Id",row].Value = this.ID;
    }
}


What I have tried:

code A : Item shows three rows, SourceTable has two rows

code B : Item shows one row, SourceTable has 0 rows

however it should be Item shows two rows, SourceTable has 1 row

what am I missing or what am I doing wrong here ?

thanks in advance
franz
Posted
Updated 21-Feb-18 17:42pm
Comments
RickZeeland 21-Feb-18 12:55pm    
And how exactly do you define SourceTable and Item ?
j snooze 21-Feb-18 17:27pm    
are you rebinding your datagrid to the datasource after adding a row?
fheyn 22-Feb-18 4:39am    
@RickZeeland
private void DataGridLayout()
{
Item.DataSource = SourceTable;
Item.Columns["ID"].Visible = false;
}

DataTable SourceTable { get { return(Database.Datatable(ItemName); }}

Item is the dgvcontrol within the mdichild frmItem. Every form represents ONE
specific Item (i.e. a person, a car, a street...)
Database is a class that incorporates sqlserver and sqlite. (this project uses sqlite). Datatable is the table of the underlying tableadapter.
RickZeeland 22-Feb-18 5:43am    
Maybe you should set dataGridView property AllowUserToAddRows = false
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.allowusertoaddrows(v=vs.110).aspx
Also I would move SourceTable.Rows.Add(dr); to the bottom of the if statement.
fheyn 22-Feb-18 12:07pm    
first of all thanks a lot for your response
when a set AllowUserToAddRows = false the dgv comes up empty and I can't add data at all (not the way I want to)
meanwhile I've replace the database-table with a local table that just contains the id-column (visible here).
I click the cell and get the same result : dgv shows three rows with the id in first row.

1 solution

I just can't spend the rest of this year trying to find out what's wrong so I've added
if (Item.Rows.Count > SourceTable.Rows.Count+1)
     Item.Rows.RemoveAt(1);

to the end of the if statement

with this it works correctly !
(actually the RemoveAt is only done when table was empty)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900