Add Records to Datagrid

G

Guest

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});
}
}
 
G

Guest

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!)
 
G

Guest

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 said:
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 said:
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});
}
}
 
J

Jeffrey Tan[MSFT]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top