At the time of populating the table, is it possible to refer to a column in a previous row?
So something like this: Refer to an other columns value in a MVC3 Razor WebGrid Except I want to refer to the same column but from a previous role.
For example, this is the column I'd like to know all values for:
//check if statement before creating it
foreach(Column("Prop") c in allRowsCollection) {
if(c.Value = "something") { //check if there is already a row with a specific value for this column
grid.Column("Prop", "example1"),
}
else {
grid.Column("Prop", "example2"),
}
}
So every time I add a new row, I'd like to compare it with the previous one.
Is this possible? If not, is there some workaround?
If you bind to against a datasource you can check the datasource and not the grid.
I would however sort this out during my sql call and omit any front end checking.
Chances are it'll be faster too because you're not moving as much data over the wire.
Related
Question:
How to empty specific column of numbers from datagridview? (i mean erase the values without header, not delete the column).
for example FROM THIS TO THAT, so i dont need to do it manually
How i created the datagridview:
i created a database table, with 5 ID's all of datatype "int", i connected the datagridview through DataSource, bindingsource. And when i put values in the table and press button:(button code below)
table1BindingSource2.EndEdit();
table1TableAdapter2.Update(database1DataSet8.Table1;
It will save the values into DataTable.
For example the first ID in the table is : Konduktivita1[µS_cm-1]
What i think is neccesery is to actually delete the values from DataTable but i dont how to do it.
It would be easier if you provided some kind of code, but let's try something like this.
var sourceList = dataGridName.ItemsSource as List<sqlTable>; //or whatever your data is called
foreach (var item in sourceList)
{
item.Konduktiva1 = null; // or 0
}
I am building LINQ to SQL and its run against SQlite database. I need to remove repeating column values from the dataset. Basically the value would be shown for the first row in the dataset and blanked for each subsequent row that had the same value.
I read about using break but I am not sure how to write it up or if it can be used. I read other threads addressing the same issue but was not able to come up with a solution for my scenario.
Should I just retrieve the dataset and suppress them in C# code?
If so I need help/pointers to do that.
If you are displaying this data in an SSRS report (.RDLC), set the HideDuplicates property of the text box that shows the data to either the name of the dataset or the grouping it's in (to show the value at the start of a new group).
You could loop through the result and set a local variable of
string lastValue = null;
foreach(var row in rows)
{
if(lastValue == row.SomeColumn)
{
row.SomeColumn = null;
}
else
{
lastValue = row.SomeColumn
}
}
Probably others ways, but hard to say without more context of what you have so far.
Have you tried using Linq to get the values from your table using the Distinct Method?
What are the essential controls before getting a piece of data from a row of a table of a dataset in c#? In order to get rid of the errors while converting a nullable data into decimal, or getting a coloumn that may not be in the datasource any more?
What do I do to validate the existence of a column?
I check if the datarow is not DBNULL.Value before a convert operation to decimal.
I do check if the coloumn exists among the coloumns of the datarow.
What I do look for?
Are there any util class to manuplate the datasets, datatables to get row or an attribute's data? Or please tell me the possible check list over datasets, datatables, datarows to be always sure about no convertion error, no such an error linking with a dataset and its child objects.
Thank you!
If you want to check if a column exists in the datatable you can simply do this:
YourDataTable.Columns.Contains("column")
If you want to check if the value of a target row is null then I would do like this:
if(!Convert.IsDBNull(YourDataTable.Rows[0]["column"]))
{
//Something
}
If you get a dataset back you proboly also want to check if there is a DataTable
If(YourDataSet.Tables.Count>0)
{
//something
}
Depending on how may rows you are expecting. If you are expecting one row the you can do this:
if(YourDataTable.Rows.Count>0)
{
//something
}
I have a gridview, bound to a datasource whose database table contains a foreign key that is associated with the database table that is used as the datasource for a dropdownlist.
What I want to do is if a certain foreignKeyId exists in gridview.datasource, to remove it from dropdownlist.datasource.
To give a clearer idea of what/why I want what I want, the user is able to add entries to the gridview (and therefore the datasource), but I don't want the user to be able to make more than one entry for a specific type. Is there a way that a linq query could do this?
pseudocode (note that I know RemoveObjects() is an invalid method)
var query = DataContext.Items.Where(item => item.TypeId == selectedTypeId);
dropDownList.DataSource.RemoveObjects(query);
Here is how I bind the dropdownlist, so maybe I could do something here to not get the items with already existing TypeId's?
dropDownList.DataSource = DataContext.Items.Select(items => new
{
items.Name,
items.TypeId,
}).ToList();
Any suggestions or answers would be great!
Have you tried using except
dropDownList.DataSource.Except(query)
In my DataGridView I'am displaying a buch of columns from one table. In this table I have a column which points to item in another table. As you may already guessed, I want to display in the grid in one column some text value from the second table instead of and ItemID.
I could not find a right example on the net how to do this.
Lets assume that I have two tables in databes:
Table Users:
UserID UserName UserWorkplaceID
1 Martin 1
2 John 1
3 Susannah 2
4 Jack 3
Table Workplaces:
WorkplaceID WorkplaceName
1 "Factory"
2 "Grocery"
3 "Airport"
I have one untyped dataset dsUsers, one binding source bsUsers, and two DataAdapters for filling dataset (daUsers, daWorkplaces).
Code which I am performing:
daUsers.Fill(dsUsers);
daWorkplaces.Fill(dsUsers);
bsUsers.DataSource = dsUsers.Tables[0];
dgvUsers.DataSource = bsUsers;
At this point I see in my dgvUsers three columns, UserID, UserName and UserWorkplaceID. However, instead of UserWorkplaceID and values 1,2,3 I would like to see "Factory", "Grocery" and so on...
So I've added another column to dgvUsers called "WorkplaceName" and in my code I am trying to bind it to the newly created relation:
dsUsers.Relations.Add("UsersWorkplaces", dsUsers.Tables[1].Columns["WorkplaceID"], dsUsers.Tables[0].Columns["UserWorkplaceID"]);
WorkplaceName.DataPropertyName = "UsersWorkplaces.WorkplaceName";
Unfortunately that doesn't work. Relation is created without errors but fields in this column are empty after running the program.
What I am doing wrong?
I would like to also ask about an example with LookUp combobox in DataGridView which allow me to change the UserWorkplaceID but instead of numeric value it will show a tex value which is under WorkplaceName.
Thanks for your time.
In my opinion, the best decision would be to use the DataGridViewComboBoxColumn column type. If you do it, you should create a data adapter with lookup data beforehand and then set DataSource, DataPropertyName, DisplayMember, and ValueMember properties of the DataGridViewComboBoxColumn. You could also set the DisplayStyle property to Nothing to make the column look like a common data column. That's it.
I don't know if you can do exactly what you want, which seems to be binding the DataGridView to two different DataTable instances simulataneously. I don't think the DataGridView class supports that -- or if it does it's a ninja-style move I haven't seen.
Per MSDN, your best bet is probably using the CellFormatting event on the DataGridView and check for when the cell being formatted is in the lookup column, then you could substitute your value from the other table. Use an unbound column for the WorkplaceName column, hide the UserWorkplaceID column and then implement the CellFormatting event handle to look up the value in the row, e.g.:
private void dgv_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
if (dgv.Columns[e.ColumnIndex].Name.Equals("WorkplaceName")
{
// Use helper method to get the string from lookup table
e.Value = GetWorkplaceNameLookupValue(
dataGridViewScanDetails.Rows[e.RowIndex].Cells["UserWorkplaceID"].Value);
}
}
If you've got a lot of rows visible, this might impact performance but is probably a decent way to get it working.
If this doesn't appeal to you, maybe use the DataTable.Merge() method to merge your lookup table into your main table. A quick glance at one of my ADO.NET books suggests this should work, although I have not tried it. But I'm not sure if this is too close to the idea suggested previously which you shot down.
As for your second question about the lookup combobox, you should really post it in a separate question so it gets proper attention.
You could make SQL do the job instead. Use a join to return a table with Workplace names instead of IDs, output that table into a dataset and use it instead.
eg.
SELECT A.UserID, A.UserName, B.WorkplaceID
FROM Users A
JOIN Workplaces B ON A.UserWorkplaceID = B.WorkplaceID
Then use its output to fill dsUsers.