PC Review


Reply
Thread Tools Rate Thread

Add Records to Datagrid

 
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      20th Sep 2005
I have two questions.

1) What is the code to check if datagrid already has a datasource?

When the user clicks "ADD" button I want to add a record to the data grid,
if the user clicks "ADD" again I want another record added to the datagrid.

2) What is the code to get the selected row of the combo box, I need colume
one and three?


Here is my code so far, I get an error message on
(this.dgPrivileges.DataSource == "")


private void cmdAddPrivileges_Click(object sender,
System.EventArgs e)
{
if (this.dgPrivileges.DataSource == "")
{
DataTable dt = new DataTable();
dt.Columns.Add("Code", typeof(string));
dt.Columns.Add("Amount", typeof(float));
this.dgPrivileges.DataSource = dt;

dt.Rows.Add(new object[]{cboPrivilege.Text,
cboPrivilege.Items.IndexOf(1)});


cboPrivilege.Focus();
}
else
{
dt.Rows.Add(new object[]{cboPrivilege.Text});
}
}

 
Reply With Quote
 
 
 
 
=?Utf-8?B?d2F2ZW1pbGw=?=
Guest
Posts: n/a
 
      20th Sep 2005
Hello Mike!

I don't understand "this.dgPrivileges.DataSource == "" "

I think it's better " this.dgPrivileges.DataSource == null "

Say me, but perhaps i don't understand your problem!

Thank's

Wavemill(sorry for my english!)

"Mike L" wrote:

> I have two questions.
>
> 1) What is the code to check if datagrid already has a datasource?
>
> When the user clicks "ADD" button I want to add a record to the data grid,
> if the user clicks "ADD" again I want another record added to the datagrid.
>
> 2) What is the code to get the selected row of the combo box, I need colume
> one and three?
>
>
> Here is my code so far, I get an error message on
> (this.dgPrivileges.DataSource == "")
>
>
> private void cmdAddPrivileges_Click(object sender,
> System.EventArgs e)
> {
> if (this.dgPrivileges.DataSource == "")
> {
> DataTable dt = new DataTable();
> dt.Columns.Add("Code", typeof(string));
> dt.Columns.Add("Amount", typeof(float));
> this.dgPrivileges.DataSource = dt;
>
> dt.Rows.Add(new object[]{cboPrivilege.Text,
> cboPrivilege.Items.IndexOf(1)});
>
>
> cboPrivilege.Focus();
> }
> else
> {
> dt.Rows.Add(new object[]{cboPrivilege.Text});
> }
> }
>

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      21st Sep 2005
The null worked, thanks for answering my first question.

The second question was, 2) What is the code to get the selected row of the
combo box, I need colume one and three?

In other words, I have a combo box and I want to get the values of the
select item the user selects.

For example if the data is in two columns, and only the first column is
shown to the user.

RF $20.00
FHL $5.00
HIP $3.00

If the user selects FHL, then I want to get "FHL" and "$5.00", which I then
can add to my datagrid.


"wavemill" wrote:

> Hello Mike!
>
> I don't understand "this.dgPrivileges.DataSource == "" "
>
> I think it's better " this.dgPrivileges.DataSource == null "
>
> Say me, but perhaps i don't understand your problem!
>
> Thank's
>
> Wavemill(sorry for my english!)
>
> "Mike L" wrote:
>
> > I have two questions.
> >
> > 1) What is the code to check if datagrid already has a datasource?
> >
> > When the user clicks "ADD" button I want to add a record to the data grid,
> > if the user clicks "ADD" again I want another record added to the datagrid.
> >
> > 2) What is the code to get the selected row of the combo box, I need colume
> > one and three?
> >
> >
> > Here is my code so far, I get an error message on
> > (this.dgPrivileges.DataSource == "")
> >
> >
> > private void cmdAddPrivileges_Click(object sender,
> > System.EventArgs e)
> > {
> > if (this.dgPrivileges.DataSource == "")
> > {
> > DataTable dt = new DataTable();
> > dt.Columns.Add("Code", typeof(string));
> > dt.Columns.Add("Amount", typeof(float));
> > this.dgPrivileges.DataSource = dt;
> >
> > dt.Rows.Add(new object[]{cboPrivilege.Text,
> > cboPrivilege.Items.IndexOf(1)});
> >
> >
> > cboPrivilege.Focus();
> > }
> > else
> > {
> > dt.Rows.Add(new object[]{cboPrivilege.Text});
> > }
> > }
> >

 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      22nd Sep 2005
Hi Cadel,

We can use ComboBox.SelectedItem to get the view of the selected row. Its
type is DataRowView. Then we can use DataRowView.Item property to get
certain column value. Sample code like this:

private void button1_Click(object sender, System.EventArgs e)
{
DataRowView drv=this.cboPrivilege.SelectedItem as DataRowView;
MessageBox.Show(drv["Lastname"].ToString());
MessageBox.Show(drv["EmployeeID"].ToString());
}

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      22nd Sep 2005
It works. Thank you.

""Jeffrey Tan[MSFT]"" wrote:

> Hi Cadel,
>
> We can use ComboBox.SelectedItem to get the view of the selected row. Its
> type is DataRowView. Then we can use DataRowView.Item property to get
> certain column value. Sample code like this:
>
> private void button1_Click(object sender, System.EventArgs e)
> {
> DataRowView drv=this.cboPrivilege.SelectedItem as DataRowView;
> MessageBox.Show(drv["Lastname"].ToString());
> MessageBox.Show(drv["EmployeeID"].ToString());
> }
>
> Hope this helps
>
> Best regards,
> Jeffrey Tan
> Microsoft Online Partner Support
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
>
>

 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      23rd Sep 2005
You are welcome

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add new records to the top of the DataGrid dbuchanan Microsoft Dot NET Framework Forms 2 29th Nov 2005 02:36 PM
How to add new records to the top of the DataGrid dbuchanan Microsoft VB .NET 2 29th Nov 2005 02:36 PM
how to get the Records (10 Pages datagrid paging) out of 50,000 Records. karunakar rao Microsoft C# .NET 4 21st Nov 2005 03:03 PM
Datagrid clear and add records =?Utf-8?B?TWlrZSBM?= Microsoft C# .NET 4 14th Sep 2005 04:12 AM
Winforms DataGrid with a 41,000+ records. Aaron Smith Microsoft ADO .NET 5 13th Dec 2004 01:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:20 PM.