473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Add Records to Datagrid

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

Nov 17 '05 #1
5 2307
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});
}
}

Nov 17 '05 #2
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});
}
}

Nov 17 '05 #3
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.

Nov 17 '05 #4
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.

Nov 17 '05 #5
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.

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ioannis Demetriades | last post by:
Hi, I am doing a VB.NET application with SQL Server 2000 and I need to implement a database search facility that will normally return lots of records. Those records will then be displayed in a...
1
by: Charlie Dison | last post by:
I am using a datagrid bound to a Dataset. The problem is when the dataset has fewer records than the page size for the height, the cells grow automatically. Thus the datagrid with one record grows...
3
by: suresh | last post by:
frenz, i need to disable the add new record mode in datarid. i just want to modify the existing records in the grid...but i dont want to add new records..how do i do that? -suresh
4
by: karunakar rao | last post by:
Hi All I need some help. I have been populated 50,000 Records in asp.net C# datagrid. Ever page has 10 records Example "<< Prev 1 2 3 4 5 6 7 8 9 10 Next>> Iam showing like that in my...
1
by: jwogick | last post by:
I'm hoping someone can help me! I just have a simple form with a datagrid displaying child records from a dataset that contains two related tables Parent table>Clients Child table>cases. using the...
3
by: Kai Zhang | last post by:
I am trying to display some database records in datagrid using dataset. the records need to be displayed are couple of thousands, but the records in database that the SQL query needs to exam are...
2
by: dbuchanan | last post by:
Hello, I am using Win forms VB 2003 The form design requires the newest records appear at the top of the DataGrid. I load my form with a stored procedure that orders the records with the...
1
by: syedrafey | last post by:
Hello, One of datagrid on my aspx page is not showing records on production server while there are records in database table. I have tested that page on my local machine and it is working fine....
3
by: dbarker1 | last post by:
Hello All, I am developing a web front end using the standard datagrid in the 1.1 framework. Currently it allows users to navigate through records 20 at a time via previous and next buttons. ...
1
by: sushant2009 | last post by:
Hi , I want to display empty databae table(actually table contains lot of records) to datagrid using dataset and dataadpter. After displaying i will fill new records in datagrid and i will save...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.