简体   繁体   中英

Saving unbound column in datagridview to a datatable

Hello I have not much experience in programming and haven't found any useful information regarding my question, that's why I need your help.

My goal is to create a datagridview that is databound by a local database (datatable), which is fully customazible by user. At first, when the user logins to main form if there was no previuos instance of editing, the datagridview will display nothing, however user can add columns by button (which specifies headertext, datatype and so on) and when added the datagridview will display the first column and then the user can edit the rows simply by clicking on the datagridview square (like MS excel). After that, the user can save the data by clicking on a save button, now the datatable will show the saved data, and the next time user will login, it will show the saved contents.

The situaton of now: I have 3 columns with data in the datatable, it is databounded to the datagridview and on debug it will show how it should be, I create the unbound column but it of course will not save after pressing the save button, thats because I have a problem with this code:

        private void button3_Click(object sender, EventArgs e) // save button
        {
            for (int j = 0; j < dataGridView1.ColumnCount; j++)
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    cmd = new SqlCommand(@"INSERT INTO Inventorius VALUES ('" + 
                         dataGridView1.Rows[i].Cells[j].Value + "')");
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
            }
         }

I'm trying to find a specific algorithm that can insert all rows and columns to the datatable that are created/deleted in the datagridview.

I think I should also look into my function that adds the column, or is it irrelavent? Ah please do tell me if the goal is suitable in doing with datagridview, or is there any more better alternatives, thank you.

I think you're going about this the wrong way

I understand you want to provide the user the ability to create columns, but there are only a certain number of columns and what columns they are is dictated by the database design. You can't let the user arbitrarily add their own column (well.. you can but this seems to be not your goal)

Because there are a set of known columns you might as well just support them all, bind them all, have them all in your datagridview and then just make them all invisible ( theColumn.Visible = false ) and when the user chooses to "add" them, just make them Visible=true . They "look" like theyve been added but in reality they were always there, wired up and working properly, just hidden


I also think you're going about your data grid/table/access the wrong way (or, the hard way)

  • Add a new dataset to your project

  • Open it, right click the surface, add a tableadapter

  • Go through the wizard, selecting a connection string, saving it, adding a select that returns rows, select * from sometable where id = @id , call the methods FillBId/GetDataById, finish

  • Save the dataset

  • Switch to your form, open the Data Sources window (View menu, Other Windows), drag the node representing your datatable, onto the form - a datagridview appears along with some other componentry that will retrieve and save data, manage current rows etc

  • Put some code into the constructor after InitializeComponent():

    foreach(DataGridViewColumn c in yourDataGridViewName.Columns) c.Visible = false;

  • Wire up some way for the user to choose columns, perhaps a listbox with checkboxes that gets its items list from the datagridview columns collection

  • Pay attention to how the back end code is structured - when you dropped the grid on the form it writes some code too; that's how to load (fill) and save (tableadapter.update) the db too

Do not iterate over a datagridview pulling values out of it; it's a UI control for showing and editing the datatable to which it is bound. If you want to get access to values, get it via the datatable. If you want to save edited alues, it's just a single line of code: someTableAdapter.Update(someDataTableNameHere)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM