C# Merge two header columns into one column? - c#

I use C#[2013] winform. I drag and drop a datagridview and design it in two cols then I select value from database to it. I now want to merge two headers into one only. The headers already displayed by designing in properties.
I want to merge the columns.
Col1 | Col2
001 | John
I want to merge into one col as
Title
001 | John
How to merge it?

What type of datasource are you binding to... You might just be better to add a new column to a datatable, or property to a class/structure that is a combined value of the two in question. Then just display that one.
If editing is done to any individual field, then update the JOINED column/property as needed.

In the class that you are using as the source for you DataGridView put a new property:
public string Title { get { return this.Col1 + " | " + this.Col2; }}
then delete those two columns and add one column for Title

Assuming you're talking about a DataGridView, there doesn't appear to be an easy built in-solution for this. Perhaps you could simply copy the data from one column to the other and split them with some sort of delimiter (perhaps a comma).
Modify then run following code after you fill your grid with data:
string data = string.Empty;
int indexCol0 = 0;
int indexCol1 = 1;
foreach (DataGridViewRow row in dataGridView1.Rows)
row.Cells[indexCol0].Value = row.Cells[indexCol0].Value + ", " + row.Cells[indexCol1].Value;
dataGridView1.Columns.RemoveAt(indexCol1);

Related

Remove duplicate rows from c# datatable using dataview.totable() not working for empty row values

I need to check existing datarows(for all columns) for possible duplicates in a datatable before adding a new row. Currently using the approach of adding a new datarow(using itemarray) to the table dt1 and comparing the row count of the existing table(dt1) and new table(dt1.dataview.totable(true)). However this does not seem to be working when the rows contain empty/null values
| Col1 | Col2 |
|------|------|
|Abc | |
| |Xyz |
When i try to add a new datarow - (Abc, ), it gets interpreted as a distinct row. Is there a way to handle such empty/null values using dataview.totable(), or linq approach to check if a row already exists(values of all columns need to be checked), or any other easier way? *the datatable columns get changed dynamically, so can't rely on column names
Gee, either always put in dbnull for a "", or always put in "" for dbnull.
If you don't adopt a set of HARD rules in your data, then coding this out will forevermore be a real pain. Computers and code HAS to have solid rules, and if you designs are well, sort of blank, but sort of null - the whole process becomes painful. This "long time" rule (be 100% defined in your rules) applies to all code - not just this example.
I think for this case? I would just "code right" though this problem if we going to ignore the above. (my choice would be always null - and use a data view against the table to filter and check in one operation.
However, due to the flip flop between "" and nulls?
Then just brute force code this out.
This works:
My table from SQL server:
SELECT FirstName, LastName, HotelName from tblHotels
Above is shoved into a data table - can have nulls.
So, now our code is thus this:
object[] MySearch = new[] { txtFirst.Text, txtLast.Text, txtHotel.Text };
bool bolFound = false;
foreach (var MyRow in MyTable.Rows)
{
if (FindRow(MySearch, MyRow) == true)
{
bolFound = true;
break;
}
}
Response.Write("<h2>Results of search = " + bolFound.ToString() + "</h2>");
}
public bool FindRow(object[] MySearch, DataRow OneRow)
{
object[] ar2;
ar2 = OneRow.ItemArray;
for (int i = 0; i <= Information.UBound(MySearch); i++)
{
if (Information.IsDBNull(ar2[i]))
{
if (MySearch[i] != "")
return false;
}
else if (MySearch[i] != ar2[i])
return false;
}
return true;
}
So, you just loop the rows - and your test converts any db null in the datatable to "". You ASSUME that your search array will ALWAYS use "" and not a dbnull in that array.
Above is less then pretty, but it not a huge amount of code.

How I can bind devexpress report table to list of strings?

I try to create report with a table and bind it to a list of string (about 10000 items).
I tried to create row in runtime, but it is very slow:
String[] ids = GetIds();
DetailTable.BeginInit();
foreach (String id in ids)
{
XRTableRow newRow = new XRTableRow();
XRTableCell newCell = new XRTableCell();
newCell.Text = id;
newRow.Cells.Add(newCell);
DetailTable.Rows.Add(newRow);
}
DetailTable.EndInit();
Also I tried to bind cell, but in report I can see only first row:
var ids = GetIds().Select(id => new {Id = id});
this.DataSource = ids;
IdCell.DataBindings.Add(new XRBinding("Text", DataSource, "Id"));
How I can fill my table?
I'd like to see your design view of the report too.
I am doing this as follows (on the Design view):
Put one xrTable to a DetailReport - that would be the table header, just fixed texts,
maybe some formatting (background, font, etc...),
I assume your table will have only one column.
insert another DetailReport (inside the first one by right-clicking on its name and selecting Insert Detail Report)
inside this second one put another xrTable of the same size, and number of columns
(there you will put the data)
click on the smart tag of this second DetailReport, add the datasource (a model, where your list is)
and point the DataMember to your List item.
I donĀ“t know if it will map itself to the string values of your list.
Probably not, so I'd change your List[[string]]
to e.g. a List[[MyOneColumnClass]]
where the MyOneColumnClass is just a class with only one public string Value {get; set;} Then you will point the DataMember to the field [Value] (just type the word in brackets into the xrTableCell text.

Dynamic column header in ASP:DataGrid

I'm looking for a way to dynamically set a column header in a datagrid(if there's a better option than Datagrid for this, please suggest).
Is a set of columns, and 1 of the columns is, for lack of a better term, a columngroup.
for example, it would look like this
Column1 | Column2 | ColumnGroup | Column 4
and ColumnGroup would be a number of columns, determined by the count of data from a stored procedure call.
For example, it will represent the number of region tabs at http://www.thehealthline.ca/ or http://www.eriestclairhealthline.ca/. As you can see, these sites are similar, but have different number of region tabs. I have to generate a table that shows specific data, one of which is related to the region tabs on the site. of which can be any number of region tabs
I realize I've rambled, I hope my problem is understandable. Would love some help from anyone that has an idea of how to do this
also, is there a way to format the columngroup like so?
|MAIN HEADER NAME|
|1|2|3|4|5|6|
You can get your ColumnGroups into a list from the stored procedure and then loop them adding them to data grid like this:
for (int i = 0; i < columnGroupList.Count; i++)
{
if (i == 0)
{
dataGrid.Columns[i].Header = 'Main Header | ' + columnGroupList[i];
}
else
{
dataGrid.Columns[i].Header = columnGroupList[i];
}
}
And you can add your other headers one at a time:
dataGrid.Columns[0].Header = ColumnHeader1;
dataGrid.Columns[1].Header = ColumnHeader2;
.
.
.

Adding rows to datagridview with combox

I have a DataGridView that I am programmatically adding rows to.
There are three columns: a TextBox column and two ComboBox columns. I've got the 1st and 3rd columns down pat because I know the text I am going to add. For the 3rd column (ComboBox) every row has the same 3 ComboBox item options.
It's the second column I'm having trouble with, where I need to have a different set of ComboBox items for each row. Is this possible?
I'll add my code below, let me know if anything's unclear. I'm looping through a list of folders, comparing the current name with a dataset value and needing to add the dataset name to the combobox items for the row if they "match".
//Loop through each local folder
string thisLocalFolder = Path.GetFileName(localFoldersArray[j].ToString());
//helpdeskCompanyNameMatchColumn.Items.Clear();
for (int i = 0; i < companyDataSet.Tables[0].Rows.Count; i++)
{
//Loop through each downloaded company name to compare
string dataComapanyToCompany = companyDataSet.Tables[0].Rows[i]["Company"].ToString();
if (dataComapanyToCompany.Contains(thisLocalFolder))
{
Console.WriteLine("Match: " + thisLocalFolder + " is close enough to " + dataComapanyToCompany);
//Add the possible match to the combobox item options, for this row
helpdeskCompanyNameMatchColumn.Items.Add(dataComapanyToCompany);
}
}

Show only selected rows in Gridview

I have DataTable object and am binding it to a gridview in C#.
I have 3 columns in the datatable, say "Flag", "Name", and "Value".
What I want to accomplish is that I want to only show the rows where "flag" fields are set to 0.
So say if I have two rows in the table,
Flag Name Value
------------------
0 tom 100
1 Jane 200
And, I only want to show "tom" and "100" on the gridview.
Is there any way I could do this without creating a new datatable?
Thanks.
Here is an example :
DataTable table = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string expression;
expression = "Date > #1/1/00#";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[i][0]);
}
You can see example Here
Probably you can take the same Datatable like : table = table.Select(...);
try creating a DataView for your DataTable and send it to your GridView instead of the DataTable. see http://msdn.microsoft.com/en-us/library/system.data.dataview.aspx

Categories

Resources