VB.NET - Automatically insert data into datagrid when enter the id in textbox

Asked By onay omas on 28-Feb-12 03:36 AM
im using vb.net on windows application.
I searching for an ideas on how to automatically generate the data in datagrid when i key in some information in a textbox and press enter, data will add up in datagrid. I want to do it for add up more than one data..





anyone has some ideas or example exactly like this?

Tq
kalpana aparnathi replied to onay omas on 28-Feb-12 03:43 AM
hi,

Use below code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Creating the columns
    Dim IDColumn As New DataGridViewColumn
    Dim ClientNameColumn As New DataGridViewColumn
    Dim AmountColumn As New DataGridViewColumn
 
    'Setting the Properties for the IDColumn
    IDColumn.Name = "ID"
    IDColumn.ValueType = GetType(Integer)
    IDColumn.HeaderText = "ID"
    IDColumn.CellTemplate = New DataGridViewTextBoxCell
 
    'Setting the Properties for the ClientNameColumn
    ClientNameColumn.Name = "ClientName"
    ClientNameColumn.ValueType = GetType(String)
    ClientNameColumn.HeaderText = "Name"
    ClientNameColumn.CellTemplate = New DataGridViewTextBoxCell
 
    'Setting the Properties for the AmountColumn
    AmountColumn.Name = "Amount"
    AmountColumn.ValueType = GetType(Decimal)
    AmountColumn.HeaderText = "Amount"
    AmountColumn.CellTemplate = New DataGridViewTextBoxCell
 
    With dtgRecords
      'Adding the Column to the DataGridView
      .Columns.Add(IDColumn)
      .Columns.Add(ClientNameColumn)
      .Columns.Add(AmountColumn)
      'Making the DataGridView ReadOnly since we don't want the user to edit the grid at the moment
      .ReadOnly = True
      'allowing only one row to be selected at a time
   .MultiSelect = False
   'restricting user capabilities on the DataGridView
   .AllowUserToAddRows = False
   .AllowUserToDeleteRows = False
    End With
  End Sub


Regards,
Somesh Yadav replied to onay omas on 28-Feb-12 03:50 AM
Hi,

refer to the below link,

same example is explained over here,

http://www.codeproject.com/Articles/13058/Data-Binding-with-Windows-Forms-2-0-Programming-Sm

Hope it helps you.
D Company replied to onay omas on 28-Feb-12 04:04 AM
Is your textbox is part of datagrid, than i may have ideas otherwise i'm not sure how onkeypress will work for it where your control does not know what the data has to populate how many columns are rows(that is possible).

there is one way that when user press a key on textbox, u serached data(or find that data from some source file and format it)but not sure immidiate reflection of it to datagrid

see this http://msdn.microsoft.com/en-us/library/h66cfhct.aspxarticcle if it is onkeypress on datagrid

hope its clear to you
D
dipa ahuja replied to onay omas on 28-Feb-12 06:38 AM
Try this way:

private void Btn1_Click(object sender, EventArgs e)
{
  //setting column count
 
  dataGridView1.ColumnCount = 1;
  dataGridView1.Columns[0].HeaderText = "column1";
 
  //adding rows
  dataGridView1.Rows.Add(textBox1.Text);
  
}