add row to datagrid with fixed variables

KriZa

Member
Joined
Jun 22, 2007
Messages
21
Programming Experience
3-5
Hi,

I use a mysql backend and I try to do following:

I got a datagridview unsing a datatable as its datasource. so I use a command_builder to update my db. works fine. now I have the problem that I try to add a row to my datagridview. but some of my columns are not visible. that is, cause the data are the same for every row (it is my foreign key). how can I set the fix-value for every NEW row? is there an object like:
datagridview.columns("CID").ValueforNewRow = CID :D:rolleyes:
or something like that?

thx 'n greets
 
is your datagrid a "child" table? I.E. do you have your "parent" data on the form, and then the grid showing related data?

for the grid to show the correct FK, i.e. the PK of the parent, you must bind the grid to the relationship between your tables, so that it automatically knows what the column value is...
 
no, it ain't.

ok, I take the information out of my form, but it is no parent-child relation, just a textbox, filled on opening that form with data out of an other table.

i think I got a solution:

Private Sub KK_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles KK.CellEndEdit
If Not (KK.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = Nothing) Then
KK.Rows(e.RowIndex).Cells("corr_KundenNr").Value = TopKNNR.Text
End If
End Sub

I just test on CellEndEdit, whether the new value is 'nothing' or not. if not, then I fill in my FK. not very elegant, I know... :eek:

I just have to think about, when to set my update-function.
If I set on RowValidated, it will update just on putting data into the grid on opening the form - thats not what I want. So it triggers right on the 'datagridview.datasource = ...' programcode.
But I have to wait for that event till I can update my table.
I need a triggerevent that comes close after validating a row, but not on filling with a datatable.
 
For your first problem you should handle the DefaultValuesNeeded event of your datagridview.

In there you can set
e.row.cells("corr_KundenNr").Value = TopKNNR.Text

Every time you click anywhere on the final row (NewRow) in your grid, this will be triggered, very handy for default values.

No idea about your second problem though.
 
If your new row in your datagrid (based on a datatable) is to have a certain value, then why dont you just set that as the default value?

e.g. in your TextBox1..TextChanged event:

VB.NET:
  Public Sub TextBox1_TextChanged(...) Handles TextBox1.TextChanged
    Me.MyDataSet.MyWhateverDataTable.CIDColumn.DefaultValue = TextBox1.Text
  End Sub

Note, we are NOT doing anything with the grid. Grid does NOT store data. In modern MVC programming, data is held in a model (DataTable) and hte grid is designated to show that model.. If you want to edit data, you edit the MODEL, not the grid!
 
Works fine!

thx for the information abot datagridviews and their datatables. I knew that, but I thougt a step too short...

thx
 
Back
Top