none
C sharp - Table problem RRS feed

  • Question

  • I want to ask is it possible to receive data in table, but first to control data, and after when i will make 3-4 or more rows with one click to transfer all rows in to my database. I'm a beginner, maybe my question is stupid, but i want to know.

    Thank You
    • Moved by eryang Wednesday, October 14, 2009 1:53 AM (From:.NET Base Class Library)
    Tuesday, October 13, 2009 1:44 AM

Answers

  • Hi Boki,

     

    For setting columns by your code, you can refer to technocrat’s previous post and you can also refer to this MSDN document,

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.aspx

     

     

    If you want to let user to control columns, you may have add one button then add code like this to the button click event,

    dataGridView1.Columns.Add(new DataGridViewColumn());

     

    For saving the data from datagridview to database by your code, you can get the connection string when you are programming. If you want the users choose target DB, you may have to ask for the user for server name, server type, username and password, and build the connection string by code.

    Then you should run the create table sql command in code. For each row in datagridview, execute insert command for it.

     

    I recommend that you learn something about ADO.NET before you writing the project. You’ll get better understanding of it and find things easier to achieve.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Sunday, October 18, 2009 7:47 AM

All replies

  • yes you can .using the sqldataadapter object ,you can release it.
    Do My Best
    Tuesday, October 13, 2009 6:47 AM
  • Please tell me in which book can i find how to use that ?

    Thanx
    Tuesday, October 13, 2009 1:34 PM
  • Hi,


    Yes. You can. get the datatable from the dataset, use the newRow() method and add columns and values to that and insert it into the database. look at the below link for more information.


    Hope this helps.
    Thursday, October 15, 2009 3:31 AM
  • Hi Boki,

     

    Technocrat is right. You can use datareader. Here is a sample using SQL Server as DB,

     

                string queryString = "SELECT OrderID, CustomerID FROM dbo.Orders;";

                DataTable dt = new DataTable();

                dt.Columns.Add("OrderID");

                dt.Columns.Add("CustomerID");

     

                 using (SqlConnection connection = new SqlConnection("connectionString"))

                 {

                     SqlCommand command =

                         new SqlCommand(queryString, connection);

                     connection.Open();

     

                     SqlDataReader reader = command.ExecuteReader();

     

                     // Call Read before accessing data.

                     while (reader.Read())

                     {

                         Console.WriteLine(String.Format("{0}, {1}",

                             reader[0], reader[1]));

                         //Do the control you want.For example, add some characters

                         string OrderID = "OID" + reader[0].ToString();

                         string CustomerID = "CID" + reader[1].ToString();

     

                         //Insert into the tabale

                         dt.Rows.Add(OrderID, CustomerID);

                     }

     

                     // Call Close when done reading.

                     reader.Close();

                 }

     

    You can get more about sqldatareader in MSDN document,

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

     

    If you have any problem with connection string, you can visit this URL,

    http://www.connectionstrings.com/

     

     

    Does this work for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, October 15, 2009 4:16 AM
  • Thats ok. But i want user to put data in data grid but not with binding source. And after that with button click to confirm what he enter and to put into database?

    Thaks about previous answers
    Thursday, October 15, 2009 2:00 PM
  • Hi,


    Take a look at this link from MSDN


    Though it takes winform as an example, you should be able to do the same even if you are using web. Hope this helps.
    Thursday, October 15, 2009 4:08 PM
  • Yes, thats what i need. Thanks technocrat_aspire . But i want customer to bind that datagrindView not myself from the code ?
    Thursday, October 15, 2009 7:15 PM
  • Hi Boki,

     

    Do you mean you want the user to create a table in datagridview then write it to database?

     

    If it is that case, I recommend you create a table in database first, because it would be easier for your coding.

     

    Please give more descriptions about the result you want.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, October 16, 2009 2:25 AM
  • Hi,

    I agree with Yichun. Can you please post a detailed description of what you are trying to achieve?
    Friday, October 16, 2009 3:31 AM
  • Sorry about my bad explanation. I want to control every column, row and cell in my datagridview. But i dont want to work with databinding and binding navigator . And i want to allow the user to fill out that datagridview and after that with only one click to confirm on button, and to put whole data in my database from datagrindview. And datagridview to be empty after that.

    Thank you for all answers
    Friday, October 16, 2009 11:30 AM
  • Hi Boki,

     

    For setting columns by your code, you can refer to technocrat’s previous post and you can also refer to this MSDN document,

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.aspx

     

     

    If you want to let user to control columns, you may have add one button then add code like this to the button click event,

    dataGridView1.Columns.Add(new DataGridViewColumn());

     

    For saving the data from datagridview to database by your code, you can get the connection string when you are programming. If you want the users choose target DB, you may have to ask for the user for server name, server type, username and password, and build the connection string by code.

    Then you should run the create table sql command in code. For each row in datagridview, execute insert command for it.

     

    I recommend that you learn something about ADO.NET before you writing the project. You’ll get better understanding of it and find things easier to achieve.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Sunday, October 18, 2009 7:47 AM
  • I solve my problem.
    Thanks for all answers
    Monday, October 19, 2009 6:29 PM