|
i am trying to write a 'windows form' database programin ms c# IDE. i can get the data into my datagrid but i have problems saving the data. When i stop the program the new data is gone. cant find tutorial code that saves the code og when i start has the stored code in the datagrid | | kimmadsen Friday, September 04, 2009 9:05 PM | Hi, here first i created one dataset and add that dataset into datagridview1 datasource. then button2 click i loop through the datagrid and added data to the collection.i hope it will help you
private DataSet CreateDataSet()
{
DataTable table = new DataTable("childTable");
DataColumn column;
DataRow row;
DataSet dataset = new DataSet();
// Create first column and add to the DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "CustID";
column.AutoIncrement = true;
column.Caption = "ID";
column.ReadOnly = true;
column.Unique = true;
// Add the column to the DataColumnCollection.
table.Columns.Add(column);
//// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "CustName";
column.AutoIncrement = false;
column.Caption = "Name";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);
dataset.Tables.Add(table);
// Create three sets of DataRow objects,
// five rows each, and add to DataTable.
for (int i = 0; i <= 10; i++)
{
row = table.NewRow();
row["CustID"] = i;
row["CustName"] = "Item " + i;
table.Rows.Add(row);
}
return dataset;
}
DataSet dsCust;
private void BindData()
{
dsCust = CreateDataSet();
dataGridView1.DataSource = dsCust.Tables[0];
}
private void Form1_Load(object sender, EventArgs e)
{
BindData();
}
private void SaveData()
{
List<GridViewItems> listItems = new List<GridViewItems>();
//Loop Through the DGV add data to gridview
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
GridViewItems items = new GridViewItems();
if (!string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["CustName"].Value.ToString()) &&
!string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["CustID"].Value.ToString()))
{<br/>
items.CustomerName = dataGridView1.Rows[i].Cells["CustName"].Value.ToString();
items.CustomerId = Convert.ToInt32(dataGridView1.Rows[i].Cells["CustID"].Value.ToString());
listItems.Add(items);
}
}
dataGridView2.DataSource = listItems.ToArray();
}
private void button2_Click(object sender, EventArgs e)
{
SaveData();
}
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you - Marked As Answer byLing WangMSFT, ModeratorSunday, September 13, 2009 11:58 AM
-
| | Gnanadurai Saturday, September 05, 2009 8:52 AM | Hi First you need to Create a SQLDataAdapter and set the insert command, update command and delete command. you need to fill the datatable using the select command of sqldataadapter. Once you have done the manipulation of data like Adding records or modifying existing data or deleting a row. you need to call the update command individually for modified rows, deleted rows, inserted rows.
For More information check the below links
- Marked As Answer byLing WangMSFT, ModeratorSunday, September 13, 2009 11:58 AM
-
| | prasams Tuesday, September 08, 2009 11:37 AM | Hi, here first i created one dataset and add that dataset into datagridview1 datasource. then button2 click i loop through the datagrid and added data to the collection.i hope it will help you
private DataSet CreateDataSet()
{
DataTable table = new DataTable("childTable");
DataColumn column;
DataRow row;
DataSet dataset = new DataSet();
// Create first column and add to the DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "CustID";
column.AutoIncrement = true;
column.Caption = "ID";
column.ReadOnly = true;
column.Unique = true;
// Add the column to the DataColumnCollection.
table.Columns.Add(column);
//// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "CustName";
column.AutoIncrement = false;
column.Caption = "Name";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);
dataset.Tables.Add(table);
// Create three sets of DataRow objects,
// five rows each, and add to DataTable.
for (int i = 0; i <= 10; i++)
{
row = table.NewRow();
row["CustID"] = i;
row["CustName"] = "Item " + i;
table.Rows.Add(row);
}
return dataset;
}
DataSet dsCust;
private void BindData()
{
dsCust = CreateDataSet();
dataGridView1.DataSource = dsCust.Tables[0];
}
private void Form1_Load(object sender, EventArgs e)
{
BindData();
}
private void SaveData()
{
List<GridViewItems> listItems = new List<GridViewItems>();
//Loop Through the DGV add data to gridview
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
GridViewItems items = new GridViewItems();
if (!string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["CustName"].Value.ToString()) &&
!string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["CustID"].Value.ToString()))
{<br/>
items.CustomerName = dataGridView1.Rows[i].Cells["CustName"].Value.ToString();
items.CustomerId = Convert.ToInt32(dataGridView1.Rows[i].Cells["CustID"].Value.ToString());
listItems.Add(items);
}
}
dataGridView2.DataSource = listItems.ToArray();
}
private void button2_Click(object sender, EventArgs e)
{
SaveData();
}
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you - Marked As Answer byLing WangMSFT, ModeratorSunday, September 13, 2009 11:58 AM
-
| | Gnanadurai Saturday, September 05, 2009 8:52 AM | well not what i expected:-))
when i created my 'windows application' program icreated the DATASET and thereafter the data then got automaticly into the DATAGRIDVIEW.(i usedthe 'Query'from the c# IDE to get the data into the database).
i use a book called 'mastering c# database programming' to learn database programming. inthis you save the chances in the database with a one commandline:
sqlDataAdapter1.Update(dataSet11, "customers");
now c# has evolved and the line is not correct anymore, but it should be the same principal. so anyone do the programming as i do??? | | kimmadsen Saturday, September 05, 2009 1:07 PM | Hi First you need to Create a SQLDataAdapter and set the insert command, update command and delete command. you need to fill the datatable using the select command of sqldataadapter. Once you have done the manipulation of data like Adding records or modifying existing data or deleting a row. you need to call the update command individually for modified rows, deleted rows, inserted rows.
For More information check the below links
- Marked As Answer byLing WangMSFT, ModeratorSunday, September 13, 2009 11:58 AM
-
| | prasams Tuesday, September 08, 2009 11:37 AM | I am not very sure which error you got in your project. You can refer to the SqlDataAdapter class to get more information.
SqlDataAdapter Class
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.aspx
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. | | Ling Wang Friday, September 11, 2009 1:36 PM |
|