Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a record to the database through TDBGrid 1

Status
Not open for further replies.

BubbaDave

Programmer
Dec 30, 2000
23
0
0
US
Hi,

I have a DBGrid (dbgMatch) that displays the values in a table with two fields: Name and MatchType.

When I set the table to active, the grid fills with the data, as it should.
I fill one listbox with the names and another listbox with the match types so I can use drag and drop directly into the grid fields.

Drag and drop requires a destination field to drop into. How can I create the empty record in the grid to have those fields available?

Thanks for any help in advance.
Dave


If it aint broke, fix it till it is.
 
I would start by looking at how the TDBNavigator buttons work. For example, if you have a DBGrid linked to a DBNavigator, you would click on the DBNavigator's Insert button to create a blank line on the DBGrid. What the Insert button does, is call the DataSet and sets its state to Insert.

I suspect that you will need to set your DBGrid's DataSet to Insert to get the blank line. I've never done drop and drag so I'm not certain how this will work.

See if any of these articles will help:
Detecting changing rows in a TDBGrid
Adding controls to a DBGrid
Editing relational tables


James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
2ffat,

Thanks for the help. All I had to do was an Append() to the dataset.

Here's the code for the drag and drop from a TDBListBox to the TDBGrid...
Code:
void __fastcall TForm1::lstMenMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
  if (Button == mbLeft)	// drag only if left button pressed
  {
    TDBListBox *pLB = (TDBListBox *)Sender; // cast to TDBListBox
    if(pLB->ItemAtPos(Point(X,Y), true) >= 0) // is there an item here?
    {
      pLB->ItemIndex = pLB->ItemAtPos(Point(X,Y), true);
      pLB->BeginDrag(false, 5);                   // if so, drag it
    }
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::dbgMenSinglesDragOver(TObject *Sender, TObject *Source,
      int X, int Y, TDragState State, bool &Accept)
{
  if (Source->InheritsFrom(__classid(TDBListBox))) // only if from a listbox
  {
    Accept = true;
    MenSingles->Append(); // add a blank record
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::dbgMenSinglesDragDrop(TObject *Sender, TObject *Source,
      int X, int Y)
{
  TGridCoord gc;
  AnsiString s;

  gc = dbgMenSingles->MouseCoord(X,Y);  // the drop field in the grid

  if ((gc.X > 0) && (gc.Y > 0))
  {
    if (lstMen->ItemIndex == -1) return; // tried to drag a blank line from the listbox

    s = lstMen->Items->Strings[lstMen->ItemIndex]; 
    dbgMenSingles->Columns->Items[gc.X]->Field->AsString = s; // add the string to the field
  }
}

Thanks for pointing me in the right direction.

Dave


If it aint broke, fix it till it is.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top