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