Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asp.NET C#

Status
Not open for further replies.

Gantonmaz

Programmer
Mar 8, 2011
2
GB
Hi Guys,

I have wrote the following web app, a simple datagrid which submits to .csv Everything is fine apart from the textbox from the template column does not write to the .csv
I want the user to be able to input the quantity they require and this appear on the .csv as well as the other information from the datagrid!
Your help would be greatly appreciated!

Code Below;

asp.

<asp:GridView ID="GridView1" runat="server" DataSourceID="as400"
AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
GridLines="None">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="IMSKU" HeaderText="SKU" />
<asp:BoundField DataField="IMDESC" HeaderText="Description" />
<asp:BoundField DataField="JFFXQT" HeaderText="Required"
ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>

Code Behind (c#)

//append to csv file
StreamWriter sw = File.AppendText("e:\\results\\decdiylayout.csv");

//seperate datagrid to comma seperated values
for (int i = 0; i < GridView1.Rows.Count; i++)
{
string strRowVal = "";
for (int j = 0; j < GridView1.Rows.Cells.Count; j++)
{
if (strRowVal == "")
{
strRowVal = DateTime.Now + "," + username[1].Remove(3)+ "," + Layout.Text + "," + GridView1.Rows.Cells[j].Text;
}
else
{
strRowVal = strRowVal + "," + GridView1.Rows.Cells[j].Text;
}
}
sw.WriteLine(strRowVal);
}
sw.Close();

}
 
it's better to work with the data from the data source rather than pull data out of formatted text (the datagrid).
I notice you are using a datasource control which only makes this more difficult/impossible.

1. replace the datasource control with code to retrieve the data.
2. bind the data to the to the grid and set the record id to the datakey on the datagrid
3. allow the user to input values into the text field
4. submit back to the server. loop through each row, query the database for the records shown on screen, and append the user input to the results. then export these results to file.

FileHelpers is an awesome tool to export data to CSV.

here is some sample code
Code:
void page_load()
{
  if postback return;
  grid.datasource = getdata();
  databind();
}

void button_click()
{
   if isvalid == false return;
   var input = gird.rows
       .cast<datagridrow>()
       .where(row=>row.rowtype == gridviewrowtype.datarow)
       .select(row => new {
                 id = grid.datakeys[row.index]["name if key"],
                 quantity = int.parse(((textbox)row.findcontrol("input id")).text)
            });

   var ids = input.select(i => i.id);
   var results = getrecordsmatching(ids);

   //file helpers
   using(var engine = new fileengine<theresult>())
   foreach(var result in results)
   {
       result.quantity = input.first(i=>i.id == result.id);
       engine.write(result);
   }
}
that's the idea anyway. manipulate the data, not the formatting. you would implement getdata() and getrecordsmatching(ids).

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top