I am currently learning C# and need some assistance.
I have created a windows form that enters data into a sql database. A Word document was created that will populate with info from the sql database on startup. The word document will be started from the windows form. The user will click the appropriate row in the datagrid and hit a "view form" button.
I need a way to tell the word document what the datagridview.Cells[0].Value is. I know of ways to move data to microsoft word after the page is opened but i need a string passed from one application to the other on startup so the form will be populated by the time it opens. Can anybody assist me with this? I greatly appreciate it
Loading
Upamanyu Roy ChoudhuryPosted Feb 21, 2013, 4:29 AM
If you do not mind you can send me your source code describing the problems areas.
I will go through it and try to fix it for you.
mail me at [email protected]
Don WalkerPosted Feb 13, 2013, 4:11 PM
Upamanyu Roy ChoudhuryPosted Feb 13, 2013, 1:36 PM
Ok so the scenario is like that:
Program 1 : consists the Gridview which has got IDS per row
Program 2: Generates or export word doc from the ID
So what you need to do is that whichever row is selected gather that ID and pass it to the word generation method.
Step 1: Bound the GridView with hyperlinks named VIEW and in the RowItemDataBound event set the command argument of the link buttons or hyperlinks as the ID of the selected value.Set the command name as say "Link"
Step 2: In the Item command event trap the "Link" button hit command and based on that get the selected ID in the e.CommandArgument and store it in a collection for later use.
Step 3: Pass the ID data as input to the export to word function.
Step 4: Exporttoword will generate the desired ourput.
If you find problems provide the code snippet. Keep me posted
Don WalkerPosted Feb 12, 2013, 11:01 AM
The word document is set on startup to run select commands based on an ID number. I want that ID number to be populated by whichever datarow was selected when the button was clicked in the C# program.
I can get the value from the datagrid, I just dont know how to combine my 2 programs to pass variables to eachother.
Thanks,
Upamanyu Roy ChoudhuryPosted Feb 12, 2013, 2:34 AM
There are many ways to access the GridView values, most common way is to get the values in RowDataBound and RowCommand event.
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(e.RowIndex == 0)
{
int i = 0;
i = Convert.ToInteger(e.Rows(0).Cells(0).Text)
}
}
}
protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e) {
}
We can set the CommandName and argument in RowDataBound and can access it from command event.Following is a MSDN link on this:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
Alternatively I did the same thing in my own in a different way, that is traverse through the gridview and then get the values as needed.
It goes like this
foreach (GridViewRow row in gridViewAsset.Rows)
{
CheckBox chkRow1 = (CheckBox)row.FindControl("chkRow");
TextBox txtDvdndRateAmt1 = (TextBox)row.FindControl("txtDvdndRateAmt");
TextBox txLstPyblDt = (TextBox)row.FindControl("txLstPyblDt");
TextBox txPyblAftrNxtDt = (TextBox)row.FindControl("txPyblAftrNxtDt");
TextBox txPyblDt = (TextBox)row.FindControl("txPyblDt");
TextBox txExDvdndDt = (TextBox)row.FindControl("txExDvdndDt");
result = ((CheckBox)row.FindControl("chkRow")).Checked;
if (result)
{
if (dtViewState.Rows[RowIndex]["Idn"].ToString() == chkRow1.Text)
{
dtViewState.Rows[RowIndex]["DvdndRateAmt"] = txtDvdndRateAmt1.Text;
if (txLstPyblDt.Text != String.Empty)
{
dtViewState.Rows[RowIndex]["LstPyblDt"] = txLstPyblDt.Text;
}
}
}
}
I have attached the code snippet.
If you find this post useful then please mark it as an answer.
With Regards,
Upamanyu
Don WalkerPosted Feb 11, 2013, 5:43 PM