Delete Table Row Interop Word - c#

I've been playing around with this, but I just can't get it to work. I have a table in Microsoft Word, and in this table is a mailmerge field which my C# program selects.
I'd like to delete the row which contains the field. I tried this:
app.Selection.SelectRow();
app.Selection.Delete();
Where app is an instance Microsoft.Office.Interop.Word.Application.
As written, this code deletes the text inside the table, but leaves an empty table row behind. How can I delete the table row itself?

When you have a Range or Selection, in most cases you can address the "containing" level like this:
app.Selection.Rows[1]
(Since I'm on a mobile device I can't check whether you can use .Delete() directly or whether you need .Range.Delete())
Similarly, Selection.Tables[1] would pick up the table around the selection and Selection.Paragraphs[1] the paragraph around the selection.
Note that, unless what you want to do simply won't work any other way, you should use the Range object. There will be less screen-flicker, execution will be faster and your code more predictable.

Related

C# Gridview: Want to create an extended gridview class that automatically hides rows with a specific value in DataKeyNames column

I'm trying to create an extended gridview class that will enable me to always have a footerrow that can be used to add data. That's the end goal.
I've tried classes that were found elsewhere on stackoverflow, but the code I found is apparently buggy (it either works when the footerrow is the only row, or it works when the footerrow is not the only row, but never both. Possibly related to the fact that I have dropdownlists in my rows). This problem was explained here: Bizarre issue with customized gridview control
The class itself is also problematic, in that the formatting is kind of messed up when the footerrow is the only row. It just doesn't look right... the color formatting is gone.
So now I'm thinking what I really want to do, is rewrite the data sources so they're based on UNION queries, so that there's always one blank row with -1 in the key column (my tables always have single incremental primary keys, so no legit row will ever have -1 in the key column), and then have a class that automatically hides any row where the value in the "DataKeyNames" field is -1.
Problem: I have no idea how to do this. :) I know how to do it in the aspx.cs code (RowDataBound event -- if(condition) {e.Row.Visible = false;}), and I know how to do it via CSS, but I don't know how to do it automatically in an extended class. I'm afraid I'm a little lost when it comes to writing extended classes.
To sum up, what I'd like to do is put an extended gridview class in my aspx code, set the usual gridview things, and have the only additional thing in my code be an added "UNION SELECT" in my datasource, and have it just automatically hide the row that the UNION SELECT provided, so that there's always at least one row in the gridview (thereby taking care of the entire footerrow not showing up problem), without me having to code anything extra in a RowDataBound event every time, because I have these gridview controls EVERYWHERE in my code.
Many thanks for any help you can provide! :)

How do I set the content of a label to a database table cell in WPF?

I have built a program in WPF using Visual Basic that allows users to select from a series of items. The list of items may change as I add more items or remove older ones. Originally, I would just add and remove these items in the project code, and rebuild the executable.
Over time, however, the list of items grew and became tedious and impractical to maintain in the code. It occurred to me that it would be easier to store the items in a database table which the program can read, and the list can just be updated by adding and removing items from this database.
I have the database built, and I've added it as a Data Source. I have the data connection set up and everything is ready to go.
What I need to know is how to set the Content property of a label to a cell from a table in the database. I want to do this in the codebehind, not in markup (XAML).
My guess is that I will need to set up a sort of query with a for loop to find the cell I want.
The name of the database is ItemsDB. It contains a table called ItemsTable, and the fields in the table have a unique ID (key) with an AutoNumber data type. The column containing the data I want is named ItemLabel.
So, to sum it all up, I want a label to show the content from a single cell in the database, specified in the codebehind. Either C# or VB is fine.
EDIT
I guess I should've mentioned that the labels themselves actually function as buttons, and I don't really want to display a ListView if I don't have to. I know it's not helpful to not have any code posted, but I don't even know where to start, so there's no code to post.
Took a while longer than I expected, hope this is still useful. So, what you want to do is iterate through a collection and create a new Label for each row.
You will need a container for the Labels, I used WrapPanel, but you can use any Panel you want. I'm replacing your Data Source with a simple DataTable, you'll get the point from this:
foreach (DataRow row in YourItemsTable.Rows)
{
Label newLabel = new Label();
newLabel.Content = row["ItemLabel"].ToString();
// If you need some events, attach the handlers here
yourParentContainerPanel.Children.Add(newLabel);
}
This should be all you need.

How to generate freeform rdlc reports with one page per row

Given a data set containing multiple rows, from within a .NET console application I need to generate a report on a single page for each row, sending those pages directly to the printer.
I am attempting to use Microsoft Report for this by attaching it to a data set and placing TextBoxes where I wish. Generating the report and sending it to the printer are not a problem. Unfortunately, the data only seems to be available in aggregates -- First, Sum, Last, Max, etc. I cannot latch the text box to a bare field.
Some poking around here and other sites seems to address this, but only when the data is presented in a table. One post even said without elaboration, "My mistake was using Text Boxes"
Am I using the wrong tool for what I am attempting to accomplish?
I ran into the same problem and managed to solve it. The solution seems a little convoluted to me so don't quote me on the "right" way to do this, but here is what I did:
Make sure you have a Dataset defined for your report.
Add a "Table" control to the report. This seems to be needed in order to iterate the rows in your Dataset.
Delete the header row and two of the default columns from the table so that you are left with a single row with a single column.
Expand the table to the width of your layout and make it as tall as you will need for your "free form" layout.
By default, there is a TextBox inside the table cell. Right-click the empty table cell and choose "delete" to remove that TextBox.
Drag a "Rectangle" control into the empty table cell. It seems to automatically "dock" to the width/height of the table cell.
Now you should be able to drag the fields from your DataSet (TextBoxes, etc) into the Rectangle to produce the desired layout.
Note that I am in the early stages of using this approach so I'm not sure if I am going to hit any walls... but for a basic report that uses TextBoxes and a page break after each "row" it seems to be working ok.
Or you try to use a list.
In the list you can arange textboxes (and other controls) as you want and they will be filled for each record in the recordset.
This work for me. :-)

.NET - DataGridView - Updating to database when user adds a row

Hey guys. I'm having an issue with using a datagridview for a user entering data that's saved to a database. Basically, I just want them to throw stuff into a row and then my code will insert the data. I know there has to be some event for what I'm trying to and I just can't figure it out.
I tried the LeavingRow event, but the problem with that is that the final value hasn't updated when this event triggers. What I mean is, say we have a row with four columns, and the user has entered data into the first three columns. They enter info in the last column, then push down to enter a new row. The insert fails, because it thinks the last column is still empty.
I also tried UserAddsRow, but had that fail because it triggers as soon as the user starts typing to add a row, so there would be only a single character in the first column of the row I was trying to add.
Does anyone have suggestions for getting these, or some other event working for my purposes?
The two inelegant alternatives I'm weighing are a) stringing together some labels/textboxes and an add button or b) using the DataGridCell_ValueChanged event, by having some nested try/catch that will try to insert first, then if it fails try to update, and then do nothing if the update doesn't work. It's grossly inefficient, but I think it would get the job done since there's only going to be a couple dozen rows tops at one time in this datagridview.
Thank you for your time.
I typically use the RowValidated Event. I use this event because it allows for a whole row to be entered / edited before trying to save the changes.
When handling the event I get the rows that have been added, updated or deleted from the dataset (DataSet.GetChanges) and then perform the appropriate actions per type (Add, Update, Delete).

Add a column to PdfPTable, iTextSharp

I'm creating a PDF document using iTextSharp. I see how to create a new table with a number of columns but I can't see anyway to dynamically add a new column. The problem I have is I'm not going to know the number of columns I need straight away, so need to keep adding them
Can somebody please enlighten me or am I going to have to re-create the table each time I need to add a column?
Thanks
Mat
Wouldn't it then make sense to create an intermediate model which contains the table you want, and is capable of then creating the PDF table for you?
I know it sounds like a lot of work, but in the long run it should help in that you would be able to dynamically alter the rows and columns as you build them, and then at the end, simply "compile" the table and spit out the PdfPTable object?
PdfPTable tables are immutable as far as column count goes once created.
The only workaround I can think of is to start with a Whole Bunch of columns and... nope that won't work either. You cannot even add cells to an existing row. I was thinking you could play around with the column spanning to mask your extra columns and adjust them as you added more cells to the rows, but that won't work either.
You must rebuild the table when adding columns. No way around it.
I strongly recommend you figure out how to determine your column count before creating the table in the first place... even if you have to "dry run" through your data. Use some intermediate format (String[][] or whatever) to keep your data, then build the table from that, not the data as it comes to you. Or at least track how many columns you'll need.
Given a huge amount of data, a single pass may not be practical/possible. But rebuilding your whole table several times can't be much better. That's really a performance tuning question that only you have the information to answer.
ITextSharp tables work in a different way to HTML tables (which I guess you're used to).
All you need to tell it is the number of columns you have and then keep adding cells.
Say you create a pdfptable with 5 columns. The 5th cell you add will be on the first row and the 6th cell will be in the 1st column on your 2nd row.
The only downside to this is if you need to add rows where not all the cells are populated but I usually get around this by just adding an empty cell or a cell with a space in it.

Categories