Windows Develop Bookmark and Share   
 index > Windows Forms General > C# 1.TableAdapter Remove Row? And 2.DataRowBuilder?
 

C# 1.TableAdapter Remove Row? And 2.DataRowBuilder?

Yo dude:

I got a dataGridView and TableAdapter.

In the following code I am successfully able to add row but got problem deleting/editing row. Please help. And what on earth is DataRowBuilder?

PLEASE ANSWER ONLY IF YOU GOT ANSWER (As I don't want to reduce the chance of getting the answer)!!!




Here' the design:

private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button buttonAddRow;
private System.Windows.Forms.TextBox textBoxName;
private System.Windows.Forms.ComboBox comboBoxSelectGender;
private System.Windows.Forms.ComboBox comboBoxSelectType;
private System.Windows.Forms.CheckBox checkBoxIsActive;
private System.Windows.Forms.Button buttonDeleteRow;




Code Snippet:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CS2008TableAdapter.ContactTableAdapters;

namespace CS2008TableAdapter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            ContactTableAdapter Adapter = new ContactTableAdapter();

            Contact.ContactDataTable Table = Adapter.GetData();

            dataGridView1.DataSource = Table.DefaultView;
        }

        private void buttonAddRow_Click(object sender, EventArgs e)
        {
            string Name = string.Empty;
            string Gender = string.Empty;
            int Type = 0;
            bool IsActive = false;

            if (textBoxName.Text != string.Empty
                && comboBoxSelectGender.SelectedItem != null
                && comboBoxSelectType.SelectedItem != null)
            {
                Name = textBoxName.Text.Trim();

                Gender = comboBoxSelectGender.SelectedItem.ToString();

                Type = comboBoxSelectType.SelectedIndex + 1;

                IsActive = checkBoxIsActive.Checked;
            }

            ContactTableAdapter Adapter = new ContactTableAdapter();

            // Only 1 Row in case of Filter is '= @Param 1'. 
            // Contact.ContactDataTable Table = Adapter.GetData(1);

            Contact.ContactDataTable Table = Adapter.GetData();

            Table.AddContactRow(Name, Gender, Type, IsActive);
            Adapter.Update(Table);

            dataGridView1.DataSource = Table.DefaultView;

            textBoxName.Text = string.Empty;
            comboBoxSelectGender.Text = "Select Gender";
            comboBoxSelectType.SelectedItem = "Select Type";
            buttonAddRow.Enabled = false;
        }

       

        private void buttonDeleteRow_Click(object sender, EventArgs e)
        {
            string Name = string.Empty;
            string Gender = string.Empty;
            int Type = 0;
            bool IsActive = false;

            if (textBoxName.Text != string.Empty
                && comboBoxSelectGender.SelectedItem != null
                && comboBoxSelectType.SelectedItem != null)
            {
                Name = textBoxName.Text.Trim();

                Gender = comboBoxSelectGender.SelectedItem.ToString();

                Type = comboBoxSelectType.SelectedIndex + 1;

                IsActive = checkBoxIsActive.Checked;
            }

            ContactTableAdapter Adapter = new ContactTableAdapter();

            Contact.ContactDataTable Table = Adapter.GetData();

            DataRowBuilder RowBuilder = null; // ??????????????????????????

            Contact.ContactRow Row = new Contact.ContactRow(RowBuilder);

            Table.RemoveContactRow(Row);
            Adapter.Update(Table);

            dataGridView1.DataSource = Table.DefaultView;

            textBoxName.Text = string.Empty;
            comboBoxSelectGender.Text = "Select Gender";
            comboBoxSelectType.SelectedItem = "Select Type";
            buttonAddRow.Enabled = false;

        }

       

        private void textBoxName_TextChanged(object sender, EventArgs e)
        {
            if (textBoxName.Text != string.Empty
                && comboBoxSelectGender.SelectedItem != null
                && comboBoxSelectType.SelectedItem != null)
                buttonAddRow.Enabled = true;
        }

        private void comboBoxSelectGender_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (textBoxName.Text != string.Empty
                && comboBoxSelectGender.SelectedItem != null
                && comboBoxSelectType.SelectedItem != null)
                buttonAddRow.Enabled = true;
        }

        private void comboBoxSelectType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (textBoxName.Text != string.Empty
                && comboBoxSelectGender.SelectedItem != null
                && comboBoxSelectType.SelectedItem != null)
                buttonAddRow.Enabled = true;
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int intCurrentRowIndex = dataGridView1.CurrentRow.Index;
                if (dataGridView1.Rows[intCurrentRowIndex].Cells[0].Value != null)
                {
                    textBoxName.Text = dataGridView1.Rows[intCurrentRowIndex].Cells[1].Value.ToString();
                    comboBoxSelectGender.SelectedItem = dataGridView1.Rows[intCurrentRowIndex].Cells[2].Value;
                    comboBoxSelectType.SelectedItem = dataGridView1.Rows[intCurrentRowIndex].Cells[3].Value;
                    checkBoxIsActive.Checked = (bool)dataGridView1.Rows[intCurrentRowIndex].Cells[4].Value;

                    buttonAddRow.Enabled = false;
                    buttonDeleteRow.Enabled = true;
                }

            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }
    }
}




TableAdapter:

Contact.xsd is created with "SELECT * FROM dbo.Contact" statement with default Get and Fill methods.

And



SQL Server:

CS2008TableAdapter.ContactTableAdapters.ContactTableAdapter

Contact.ContactDataTable

USE [master]
GO

IF EXISTS (SELECT * FROM SYS.DATABASES WHERE NAME = N'ContactDB')
DROP DATABASE [ContactDB]
GO

CREATE DATABASE [ContactDB]
GO

USE [ContactDB]
GO

IF EXISTS (SELECT * FROM SYS.OBJECTS WHERE NAME = N'Contact')
DROP TABLE dbo.Contact
GO

CREATE TABLE dbo.Contact
(
Id INT IDENTITY(1,1) NOT NULL,
Name NVARCHAR(50),
Gender NVARCHAR(10),
Type INT,
IsActive BIT,
CONSTRAINT PK_Contact_Id PRIMARY KEY (Id)
)
GO

SELECT * FROM dbo.Contact

INSERT INTO dbo.Contact
VALUES
(
'Bill Gates',
'Male',
1,
1
)
GO

INSERT INTO dbo.Contact
VALUES
(
'Melinda Gates',
'Female',
2,
1
)
GO

SELECT * FROM dbo.Contact

  • Edited byrecherche Monday, October 05, 2009 7:36 PMtypo
  • Edited byrecherche Tuesday, October 06, 2009 8:40 AMtypo
  • Edited byrecherche Wednesday, October 07, 2009 4:42 AMtypo
  • Edited byrecherche 16 hours 10 minutes agotypo
  •  
recherche  Monday, October 05, 2009 7:33 PM

Hi recherche,

Thanks for providing so much information about your issue. In your code, I cannot understand the code snippet below in the buttonDeleteRow_Click function:

ContactTableAdapter Adapter = new ContactTableAdapter();
Contact.ContactDataTable Table = Adapter.GetData();
DataRowBuilder RowBuilder = null; // ??????????????????????????
Contact.ContactRow Row = new Contact.ContactRow(RowBuilder);
Table.RemoveContactRow(Row);
Adapter.Update(Table);

Could you please explain a little more?

From my experience, if we want to delete a row, we would often follow the steps below:

1.    Find the row in the DataTable.

2.    Call the Delete method of the row.

3.    Call the Update method of the DataAdapter to update the data to database.

The code snippet below shows the implementation of the steps above:

//Create adapter and load table.
ContactTableAdapter Adapter = new ContactTableAdapter();
Contact.ContactDataTable Table = Adapter.GetData();
//Find the rows need to be deleted.
DataRow[] rows = Table.Select(string.Format("Name='{0}' and Gender='{1}' and Type={2} and IsActive={3}",
    Name, Gender, Type, IsActive ? 1 : 0));
//Delete the rows.
if (rows.Length > 0)
{
    foreach (DataRow row in rows)
    {
        row.Delete();
    }
}
//Update data to database.
Adapter.Update(Table);



Please let me know if this does not help.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer byrecherche 16 hours 5 minutes ago
  •  
Aland Li  Thursday, October 08, 2009 3:10 AM

Hi recherche,

Thanks for providing so much information about your issue. In your code, I cannot understand the code snippet below in the buttonDeleteRow_Click function:

ContactTableAdapter Adapter = new ContactTableAdapter();
Contact.ContactDataTable Table = Adapter.GetData();
DataRowBuilder RowBuilder = null; // ??????????????????????????
Contact.ContactRow Row = new Contact.ContactRow(RowBuilder);
Table.RemoveContactRow(Row);
Adapter.Update(Table);

Could you please explain a little more?

From my experience, if we want to delete a row, we would often follow the steps below:

1.    Find the row in the DataTable.

2.    Call the Delete method of the row.

3.    Call the Update method of the DataAdapter to update the data to database.

The code snippet below shows the implementation of the steps above:

//Create adapter and load table.
ContactTableAdapter Adapter = new ContactTableAdapter();
Contact.ContactDataTable Table = Adapter.GetData();
//Find the rows need to be deleted.
DataRow[] rows = Table.Select(string.Format("Name='{0}' and Gender='{1}' and Type={2} and IsActive={3}",
    Name, Gender, Type, IsActive ? 1 : 0));
//Delete the rows.
if (rows.Length > 0)
{
    foreach (DataRow row in rows)
    {
        row.Delete();
    }
}
//Update data to database.
Adapter.Update(Table);



Please let me know if this does not help.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer byrecherche 16 hours 5 minutes ago
  •  
Aland Li  Thursday, October 08, 2009 3:10 AM
Thanks, you hit the nail on its head!

BTW, what on earth is DataRowBuilder? What is the usage of DataRowBuilder? As a matter of fact, DataRowBuilder was prompted by intellisense. How can it be used in code (if not directly)?

Thanks again.
recherche  16 hours 5 minutes ago
Hi recherche,

I did not use DataRowBuilder too much. Actually, using this class is not suggested. You can get more about DataRowBuilder:
http://msdn.microsoft.com/en-us/library/system.data.datarowbuilder.aspx

You can also create another thread to ask how to use DataRowBuilder in ADO forum:
http://social.msdn.microsoft.com/Forums/en/adodotnetdataset/threads

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  7 hours 42 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• Incremental search in GridView by sorted column
• apply WinXp visual style to winforms
• can't PropertyGrid support multi column?
• The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.
• ListBox right click that selects item
• Again Winform Datagrid: Individual row height and Vertical Scrollbar
• Toolstrip
• Windows Time in a TextBox control
• Updating Multiple Data Set
• Small windows or dialogs