Results 1 to 7 of 7

Thread: datagrid in VS2003

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    datagrid in VS2003

    hello!
    when i click the datagrid, how can i view the current selection
    pls. see attached jpeg
    tnx.
    Attached Images Attached Images  

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagrid in VS2003

    Asuming that myDataTable is bound to myDataGrid:
    C# Code:
    1. DataRowView row = myDataTable.DefaultView(myDataGrid.CurrentRowIndex);
    You can also get it through a BindingContext and CurrencyManager but I've never actually done that so I'll leave that to someone else to demonstrate, or you could search the forum for existing examples.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: datagrid in VS2003

    i've done this in VS2005 its easy as this:

    private void dataGridView1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
    {
    try
    {
    DateTime dt = DateTime.Parse(dataGridView1.CurrentCell.ColumnNumber["DATE_HIRED"].Value.ToString());

    txtEmpID.Text = dataGridView1.CurrentRow.Cells["EMP_NUMBER"].Value.ToString();
    txtLname.Text = dataGridView1.CurrentRow.Cells["LAST_NAME"].Value.ToString();
    txtFname.Text = dataGridView1.CurrentRow.Cells["FIRST_NAME"].Value.ToString();
    txtMI.Text = dataGridView1.CurrentRow.Cells["MID_NAME"].Value.ToString();
    mskDateHired.Text = dt.ToString("MM/dd/yyyy");
    cboSectionCode.Text = dataGridView1.CurrentRow.Cells["ORG_SECTION_CODE"].Value.ToString();


    btnEdit.Enabled = true;
    txtEmpID.Enabled = false;
    txtFname.Enabled = false;
    txtLname.Enabled = false;
    txtMI.Enabled = false;
    mskDateHired.Enabled = false;
    cboSectionCode.Enabled = false;
    btnSave.Enabled = false;
    btnDelete.Enabled = true;
    }
    catch
    {
    return;
    }
    }


    but in VS2003 its different approach

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagrid in VS2003

    It's got nothing to do with 2003 and 2005. It's got to do with the fact that the DataGridView and DataGrid are two different controls. What's the problem with what I suggested previously?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: datagrid in VS2003

    ok, for now i solved my own problem tnx anyways.
    pls. see code below

    private void dataGridView1_CurrentCellChanged(object sender, System.EventArgs e)
    {
    dataGridView1.Refresh();
    int iRownr=this.dataGridView1.CurrentCell.RowNumber;
    int iColnr=this.dataGridView1.CurrentCell.ColumnNumber;
    object cellvalue1=this.dataGridView1[iRownr, iColnr];

    object cellvalue2=null;
    object cellvalue3=null;
    object cellvalue4=null;
    object cellvalue5=null;
    object cellvalue6=null;

    try
    {
    cellvalue2=dataGridView1[iRownr, iColnr+1];
    cellvalue3=dataGridView1[iRownr, iColnr+2];
    cellvalue4=dataGridView1[iRownr, iColnr+3];
    DateTime MyDate = DateTime.Parse(dataGridView1[iRownr, iColnr+4].ToString());
    cellvalue5 = MyDate.ToString("MM/dd/yyyy");
    cellvalue6=dataGridView1[iRownr, iColnr+6];
    txtEmpID.Text=cellvalue1.ToString();
    txtLname.Text=cellvalue2.ToString();
    txtFname.Text= cellvalue3.ToString();
    txtMI.Text=cellvalue4.ToString();
    mskDateHired.Text=cellvalue5.ToString();
    cboSectionCode.Text=cellvalue6.ToString();
    }
    catch
    {
    MessageBox.Show("Please don't click the column, if you want to change the data click the pointer or \n search again to navigate the data.","Unexpected Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

    }
    }

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagrid in VS2003

    Are you determined to ignore my previous suggestion for some reason?
    vb Code:
    1. DataRowView row = myDataTable.DefaultView[myDataGrid.CurrentRowIndex];
    2.  
    3. txtEmpID.Text = (string)row["EmpID"];
    4. // etc.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: datagrid in VS2003

    DataTable myDataTable = (DataTable) dataGridView1.DataSource;
    DataRowView row = myDataTable.DefaultView[dataGridView1.CurrentRowIndex];

    MessageBox.Show((string)row["EMP_NUMBER"]);


    tnx a lot it works!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width