I don't know a good way to explain this but what I have so far is I created a dataset and called it Database and created a table in it. Then I used a bindingsource and defined Database as its datasource.
Then I put the bindingsource as my datagrids data source and I put my textboxs text to the respective column in the bindingsource (e.g. textbox1 text=bindingsource - Name) so I can update my xml so I can click on the row and it fills the textboxes with the rows info and as i edit the text box it changes the data in the rows on the grid view so far an edit and update.
But I want to be able to have the option to update or not. So instead of I type and it changes I want to be able to fill in the boxes then choose to press update or cancel like a normal edit form.
This is my form
It's as if I could do something like
string = textboxname
then when I press update it then does selected row column "name" = string
ok so i figured it out thanks to devTimmy. i was looking in the wrong place and with the wrong ideas.
iv now done it so it takes the selected row index and uses that to fill in the correct row when its told which cell to fill. i didn't think to tell it which cell before so it failed and i thought no more of it.
here is my code.
int i;
i = dataGridView1.CurrentRow.Index;
dataGridView1.Rows[i].Cells[0].Value = Nametb.Text;
dataGridView1.Rows[i].Cells[1].Value = Locationtb.Text;
dataGridView1.Rows[i].Cells[2].Value = Infotb.Text;
dataGridView1.Rows[i].Cells[3].Value = dayvisittb.Text;
database1.WriteXml("Database.xml");
Related
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)
I'm working with DataGridView in C#. I fill it with a table from database using BindingSource. After filling the DataGridView I need to add a row of checkboxes that should be first in the table, to enable data filtering in future.
I mean: for example after clicking "Load" button I get DataGridView filled with 4 columns of data and the first row should consists of checkboxes.
I confused. I use
DataGridViewCheckBoxCell checkCell = new DataGridViewCheckBoxCell();
SourceDGV.Rows.Add(checkCell);
I mean here:
But after clicking load button it says You can't add rows to databounded DataGridView
Sorry if ecxeption translated wrong, I translated it from Ukrainian.
Any help appreciated.
Am afraid, there is no concept of whole rows to be of some type(checkbox in your case) in DataGridView. It is other way around. You can have whole column to be of checkbox type. You'd use DataGridViewCheckBoxColumn for that.
Showing checkboxes in whole row even makes no sense for me: Assume you have data bounded Person object to the DataGridView. So there will be columns like Name, Age, Address. Now, on what basis you show checkbox in the row? How can a Name be converted to a bool(which is what checkbox needs as a value)?
One workaround I can think of would be, handle custom painting, If First row paint checkboxes over there and manually handle their click events. But am pretty sure it's not gonna be easy.
I have a Winform App where I want a user to be able to enter some stock corrections against a list of products which will in turn create a database record for each of those corrections.
Using a combobox, I got it so each item would have to be selected in turn. It works great, but is not very user friendly. Ideally, I'd like a list of products with an empty column to enter the corrections, click submit and ya done.
I looked at using a datagrdiview. I can generate the list of products, but I've no idea on how to create the 'entry column'.
Any suggestions on the path to take?
Cheers,
Numb
EDIT
Here is a mock up of what I would like to do to make it as clear as possible without showing code...
If you want use a ”Submit” button to save a new stock correction, then
approach will be:
-Create a datagridview with products names and empty cell for stock correction
-After ”Submit” button was clicked read ”Correction” columns values, if value exists update database…
Here how you can do it:
Create a datagridview in designer dgvProductCorrections,
then create with designer a two columns in this datagridview
dgvProductCorrection_Product and dgvProductCorrection_Correction.
Put this(or your) columns names in Column .Name property
Then, I assume that you have a class Product where exists a property .Name,
you will need to put a name of this property(in my example will be Name) in .DataProperty of column
Above can be done with designer or in code(Constructor)
Adding a list products List<Product> lstProducts; in datagridview will be as:
dgvProductCorrections.DataSource = lstProducts; //(this may be in Form_Load)
Rememeber before adding a list set a datagridview property AutoGenerateColumns to False
dgvProductCorrections.AutoGenerateColumns = false;
in Button_Click event handler of your Submit button put a code where you loopin through a all rows and reading a correction values. After you can update a database with those values
//Code in Button_Click
{
foreach(DataGridVewRow dgvr in dgvProductCorrections.Rows)
{
Decimal fCorrection;
//Check if value exists and it can be used. Add own other checks
if(dgvr.Cells(this. dgvProductCorrection_Correction.Name).Value != null && Decimal.TryParse(dgvr.Cells(this.dgvProductCorrection_Correction.Name).Value.ToString(), fCorrection) = True)
{
//Here you can put a update code, or save a correction in list and then update all by one update call
}
}
}
I have an application that displays the data of a sql database in a dataGridView
via a dataSet. I was wondering if there was a way of updating the database by modifying the gridViewDirectly, like passing the data from the database to the grid, but inverted, instead of creating a new form that displays the editable info in textboxes and then the user changes what he has to change and clicks update.
Is this possible?
the DataGridView had a already feature to DataSet with BindingSource, TableAdapter and BindingNavigator Control by manipulating the data from database to DataGridView just drag and drop to your Form no more codes.
Create a DataSet
Drag Table to DataSet
Go to "Data Sources" panel
Drag the Table to your Form.
And thats it! as you can see the "Save" and "Delete" button from BindingNavigator this will be your function to save, update and delete the data from your database using the DataGridView with DataSet
i apologize for my short tutorial and not very clear :)
you can put a button column in datagridview and when you press the button you can begin to edit dgv cells.
info: how can i add two buttons to a single datagrid column dynamically?
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewbuttoncolumn.aspx
Define DataGridView Column type Programmatically
If you need any other information about that please feel free to say
Edit:
You can do like this :
DataGridViewButtonColumn clbt_delet = new DataGridViewButtonColumn();
clbt_delete.HeaderText = "DELETE"
clbt_delete.Text = "Delete";
clbt_delete.UseColumnTextForButtonValue = true;
dataGridView1.Columns.Add(clbt_delete);
Out of curiosity is it possible to open a form based on row selection in a datagrid? I would also need the form to show information based on the username in the datagrid. The persons username is included within the row of the datagrid.
You will have to code this, but yes, it is possible.
First, populate your DataGrid with data that you can handle.
On the DataGrid's Selection Changed event, read that data, create the form you want to show (if it does not already exist), and display it using Show().
This would be like a typical Menu program.
You can handle this under the following event
dataGridView1_CellClick
Get the CurrentCell value of the datagridiview
Check for the username exists or not as per you asked and show the respective form
Sample code:
if (this.dataGridView1.CurrentCell != null)
{
string strusrname=dataGridView1.CurrentCell.Value.ToString();
//Here find out for the user name from the string as you get the currentcell value of the datagridview
// Raise the corresponding form as per you required
}
Not really sure if this is what your after, as i don't no if you want to show the data on another pre-built form or create a new one, but here it goes.
This way you won't even need to worry about the row selected, assuming you have the username of the person bound to the datagrid you can create a hyperlinkcolumn like this:
<asp:HyperLinkcolumn DataNavigateUrlField="Username"
DataNavigateUrlFormatString="PersonForm.aspx?Username={0}"
HeaderText="More Details"
Text="View Person Details" />
Then the PersonForm can load the persons details. Or if you would like some help on how to catch the selected row on itemcommand then let me no.
Hope this helps.
EDIT: after your winforms tag update you may be interested in this: DataGridViewLink On MSDN
The general code is:
DataGridViewLinkColumn links = new DataGridViewLinkColumn();
links.UseColumnTextForLinkValue = true;
links.HeaderText = ColumnName.ReportsTo.ToString();
links.DataPropertyName = //Set your field here.
links.ActiveLinkColor = Color.White;
links.LinkBehavior = LinkBehavior.SystemDefault;
links.LinkColor = Color.Blue;
links.TrackVisitedState = true;
links.VisitedLinkColor = Color.YellowGreen;
DataGridView1.Columns.Add(links);
Once you have added a link you can catch it using DataGridView1_CellContentClick and do what you want with it, ie open a new form or alter the current one.