Delete multiple rows from Grid - c#

How do I delete multiple rows from XtraGrid ? I am using DevEx version 10.2. Any tutorials available ?

You can get the selected rows using the GetSelectedRows method (nicely named right?).
Then you can just use a loop to go through the rows and delete the ones selected.
Also, I found this "tutorial" which might be what you are looking for: http://www.devexpress.com/Support/Center/KB/p/A234.aspx
It talks about the ColumnView.DeleteSelectedRows method, though it's only available in newer versions of XtraGrid.

you can't delete more than one row at time but we can Trick on the c#.
actually when you delete one row from the gridview by using this statement GridData.Rows.RemoveAt(RowIndex);
the rows count is decremented by one and the row index will change so you must be careful about that.
here you are, first you can create an integer variable which equal zero and when you remove one row increment it by one.
second, put the index of the rows you want to delete in an array and then use for loop to delete the rows but again take care when you send your parameter.
int deletedValue = 0 ;
for (int j = 0; j < counter; j++)
{
if (Array.binarySearch(myArray,j)){
GridData.Rows.RemoveAt(j-deletedValue); deletedValue++;`
}
else {//The non deleted rows}}
}

Related

SSIS need to add same rownumber to rows, then reset when it finds a certain value in a row, from file source

I have a file source where the data is not in normalized form with any sort of primary key value or repeating group value. I am using Merge Join to put the multiple rows into one merged row. I need to apply some row numbering so that I have a join between the multiple rows, to get them into the one single row for the merge join.
Here is what the source data looks like:
Data Rows:
MSH|BLAH|||BLAHBLAH15|BLAHZ|||
EVN|MOREBLAH|BLAHBLAH11|BLAHY|||
PID|BLAHXX|BLAHBLAH655|BLAHX|||
PV1|BLAHX2|BLAHBLAH42|BLAHX|||||||||
DG1|1||84|XXXX||A
IN1|1||11400|TEST
IN1|2||20100|TEST2
MSH|BLAH2|BLAHBLAH5|BLAHZ|||
EVN|BLAH6|20220131123100
PID|BLAHGG|BLAH222|BLAHX|||
PV1|PV1|BLAHX2|BLAHBLAH42|BLAHX||||||||20220101|
DG1|1||84|XXXX||A
DG1|2||84|XXXX||A
IN1|1||11600|TEST2
What is consistent is that there is always an MSH line as the header, and everything below it belongs with the MSH line at the top.
So I'm trying to accomplish this by applying a row numbering as below, where it goes from 1,1,1,1 to 2,2,2,2,2 incrementing by one wherever it finds the MSH line, as per below:
Data Rows: Numbering Needed:
MSH|BLAH|||BLAHBLAH15|BLAHZ||| 1
EVN|MOREBLAH|BLAHBLAH11|BLAHY||| 1
PID|BLAHXX|BLAHBLAH655|BLAHX||| 1
PV1|BLAHX2|BLAHBLAH42|BLAHX||||||||| 1
DG1|1||84|XXXX||A 1
IN1|1||11400|TEST 1
IN1|2||20100|TEST2 1
MSH|BLAH2|BLAHBLAH5|BLAHZ||| 2
EVN|BLAH6|20220131123100 2
PV1|PV1|BLAHX2|BLAHBLAH42|BLAHX|||||| 2
DG1|1||84|XXXX||A 2
DG1|2||84|XXXX||A 2
IN1|1||11600|TEST2 2
I can't use a specific row count to reset the number, ie: Every 5 rows increment the row numbering, because it's an inconsistent number of rows each time. In the example above, the first set is 7 rows and the 2nd set is 6 rows. I have to do my incrementing by the presence of the "MSH" row value, and apply the same number on down until it finds the next "MSH". I know that have to use a script task (preferably in C#) to generate this row number since my source is a file. But I just can't seem to find the right logic that will do this, since my data doesn't have a repeating key for each row that I can partition by.
This is what I would do to meet your requirements:
Read entire row in a flat file viewer
Go into a script task (source). I forgot to mention to add the row as read only.
Set up an output for each type.
Go into the code.
Set up a variable outside of main processing (in startup)
int counter = 0;
In main row processing:
string[] details = Row.Split('|');
switch(details[0])
{
case "MSH":
counter++; //increment counter
OutputBufferMSH.AddRow();
OutputBufferMSH.Counter = counter;
OutputBufferMSH.Col1 = details[1];
// Repeat for each detail Column
break;
case "EVN":
OutputBufferEVN.AddRow();
OutputBufferEVN.Counter = counter;
OutputBufferEVN.Col1 = details[1];
// Repeat for each detail Column
break;
//Repeat for each row type
}
I personally would not use this counter approach but actually load the MSH row and return the identity column to replace the counter.
Honestly, I would do the whole thing in a console application instead and use a StreamReader to load the flatfile. Readlines and then use the above logic to push the data into DataTables and use a Bulk Insert to load the data. But the above is the solution to do this in SSIS.
There is a lot to unpack here if you are not familiar with C# or the script task object itself.

Make outline in Excel OpenXML

I'm adding rows to a spreadsheet using OfficeOpenXml row copy method, Every time I add a row, I set the outlinelevel of the row (olLevel incremented outside of loop below). I'm testing with 12 total rows, attempting to place in two groups of six.
for (int j = 0; j < employeeRecordCount; j++) {
detailRow.Copy(outputSheet.Cells[detailRowStart + j, columnA]);
outputSheet.Row(detailRowStart + j).OutlineLevel = olLevel;
startingRow++;
lastdetailRow++;}
When I open the spreadsheet, all rows are in one group of 12 and the last six rows are grouped but nested within the larger group (the six collapse/expand independently of all 12). If I repeat 6 of the records from my input file, those records are grouped together as 12 and the other 6 are nested.
I know I'm forgetting to set something besides outlinelevel to keep the groups seperate from each other but I cannot figure it out.
Can anyone see what I am missing?
The issue was that every detail row began with a header row. Excel was grouping all the rows on one level and creating another nested level to group rows under the next unique header row.
I edited the code to not apply an outline level to the first detail record (header row) and the rows are no longer nested.

Reading from datatable; how to trap error on last line

I have an application that reads an entire Excel file into a datatable. I need to retrieve subsets of data from this table into separate datatables. I do this by looping down the cells of the Excel table until I find a blank cell.
The problem is that the last line of the longest column of the Excel table (in other words, the last row of the Excel table) always errors out, with "There is no row at position " followed by whatever the last row of the longest column is.
Here is a scaled-down version of my code that gives me the error:
do {
string MyString = dtExcel.Rows[i][11].ToString();
} while (dt.Rows[i][11].ToString().Length > 0);
Where i is the row counter and [11] is the column I need to save. It works perfectly until the last row of the longest column, and then bombs out.
I've tried checking to see if dtExcel.Rows[i][11] is null, or if the ToString() length is zero, but I can't figure out how to trap this error because the mere act of trying to read it causes the error.
I guess my question is, is there a way of checking to see if this row even exists before I try to check it for null or turn it into a string, or whatever?
Hopefully this is clear. Thanks for any help.
Instead of using a while loop checking if the row is empty, use
for(int i=0;i < dt.Rows.Count ;i++){
//...
}
to loop through the rows.
By doing that, it'll know when to stop in advance, and you will not get an
Index out of Bounds Exception
Try reading your datatable this way:
StringBuilder sb = new StringBuilder();
foreach( var r in dt.AsEnumerable())
{
if(string.IsNullOrEmpty(r[11])) break;
sb.Append(r[11]);
}
return sb.ToString();

Adding rows to second column ONLY - populating data using a for loop

I calculate the amount of rows I want to have in my second column using a for loop based on reading how many records a file has that has been opened. I have researched and tried various solutions but nothing works, yet it seems so simple. Below is my current code where I retrieve the file's length and do a quick sum, entering a for loop where (at the moment) I am only able to populate the first column.
long Count = 1;
FileInfo Fi = new FileInfo(file);
long sum = (Fi.Length / 1024) - Count;
for (int i = 0; i < sum; i++)
{
DataGridView1.Rows.Add(Count++);
}
I'm not sure how to do it but I know the above code adds to the first column by default - I don't know how to modify it. I know by:
DataGridView1.Rows.Add("a","b");
... The 'b' value is displayed in the second column, but I don't want anything for now in the first where 'a' is.
I have looked at insert a row with one column datagridview c# but it is related to merging columns, again, I don't want this.
DataGridView1.Rows.Add("",Count++);
Works to an extent, but is not the right way to do it. I'm going to be adding data to the first column later on.
If you want to omit the value for the first column, just add null or DBNull.Value, e.g.:
DataGridView1.Rows.Add(DBNull.Value, Count++);
This way, the first column will be empty while the second columns contains the value of Count.

C# datatable take rows one at a time

I have a datatable containing certain columns. I am able to display them in a repeater as a whole. However, what I want to do is to display the row, one by one. I want to create like a go next and show previous button. I did something like the following:
myDataTable.Rows.Cast<DataRow>().Take(1).CopyToDataTable();
This is giving me the first row. Now how can I use this concept to get the next row, (row 2), then row 3..... till row n. The rows being returned are different for different cases. It is an web application.
To get a different row, you just need to Skip some:
myDataTable.Rows.Cast<DataRow>().Skip(n).Take(1).CopyToDataTable();
where n is how many rows you want to skip. So, for the second record n would be 1.
I greatly disagree with the use of CopyDataDataTable(), but it would require a lot of knowledge of your code base to provide a better approach.
I would select it from the database instead, however, use Skip(n).Take(1):
var row3 = myDataTable.AsEnumerable().Skip(2).Take(1).CopyToDataTable();
Introduce the use of .Skip():
myDataTable.Rows.Cast<DataRow>().Skip(0).Take(1).CopyToDataTable();
Now you can simply track which record the user is currently viewing, and update the value sent to the .Skip() method. For example, if the user has pressed "next" 5 times, you've incremented the value 5 times:
myDataTable.Rows.Cast<DataRow>().Skip(5).Take(1).CopyToDataTable();
Keep a counter and use Skip(n-1).Take(1) for nth record.

Categories

Resources