简体   繁体   English

Datagrid模板列提交到.CSV

[英]Datagrid Template Column submit to .CSV

I have a GridView that exports to .csv just fine with the code below. 我有一个GridView可以将下面的代码导出到.csv中。 The problem is that I have a TemplateColumn with a TextBox which isn't wrote to the .CSV at all! 问题是我有一个带有文本框的TemplateColumn,根本没有写入.CSV!

Can anyone help me with this?? 谁能帮我这个?? its driving me mad! 它让我发疯!

Any help here would be great! 这里的任何帮助将是巨大的!

<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>

protected void Button1_Click(object sender, EventArgs e)
{

    //Get Username
string[] username = Request.ServerVariables["LOGON_USER"].ToString().Split('\\');
    globalfunctions getAttribute = new globalfunctions();

    string storeEmail = getAttribute.getADattribute(username[1], "mail");

    //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[i].Cells.Count; j++)
    {
    if (strRowVal == "")
    {
        strRowVal = DateTime.Now + "," + username[1].Remove(3)+ "," + Layout.Text + "," + GridView1.Rows[i].Cells[j].Text;
    }
    else
    {
        strRowVal =  strRowVal + "," + GridView1.Rows[i].Cells[j].Text;
    }
    }
    sw.WriteLine(strRowVal);
}
sw.Close(); 

This worked: 这工作:

         //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[i].Cells.Count; j++)
            {

                GridViewRow row = GridView1.Rows[i];

                TextBox Qty = (TextBox)row.FindControl("TextBox2");
                String Quant = Qty.Text;


                if (strRowVal == "")
                {
                    strRowVal = DateTime.Now + "," + username[1].Remove(3)+ "," + Layout.Text + "," + Quant + "," + GridView1.Rows[i].Cells[j].Text;
                }
                else
                {
                    strRowVal =  strRowVal + "," + GridView1.Rows[i].Cells[j].Text;
                }
            }
            sw.WriteLine(strRowVal);
        }
        sw.Close(); 

You need to get the textbox control and get its value. 您需要获取文本框控件并获取其值。

Something like... 就像是...

         TextBox txt = GridView1.Rows[i].Cells[j].FindControl("textboxid");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM