如何使用C#在TableCell中添加文件上载控件

时间:2013-02-07 08:02:42

标签: c# asp.net

我正在使用TableCell和GridViewRow

在代码中创建网格视图

从数据库表中提取数据。

我的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class gv1 : System.Web.UI.Page
{
    SqlConnection cn;
    SqlCommand cmd;
    DataSet ds;
    SqlDataAdapter da;

   protected void Page_Load(object sender, EventArgs e)
  {
     cn = new SqlConnection("Data Source=AMIR-PC\\MOHEMMAD;Initial Catalog=CRM_InvestPlus;Integrated Security=True");

  }
  protected void Button1_Click(object sender, EventArgs e)
 {
    cn.Open();
    cmd = new SqlCommand("Select * from Customer_Master", cn);
    da = new SqlDataAdapter(cmd);
    ds = new DataSet();
    da.Fill(ds);
    cn.Close();

    GridView gr1 = new GridView();
    Table t = new Table();
    gr1.Controls.Add(t);

        for (int i = 0; i < 6; i++)
       {
           GridViewRow row = new GridViewRow(i, i, DataControlRowType.DataRow, DataControlRowState.Normal);
           TableCell cell1 = new TableCell();
           TableCell cell2 = new TableCell();
           TableCell cell3 = new TableCell();

           cell1.Text = ds.Tables[0].Rows[i][0].ToString();
           cell2.Text = ds.Tables[0].Rows[i][1].ToString();
           cell3.Text = ds.Tables[0].Rows[i][2].ToString();
           row.Cells.Add(cell1);
           row.Cells.Add(cell2);
           row.Cells.Add(cell3);

            gr1.Controls[0].Controls.AddAt(i, row);    

        }
        form1.Controls.Add(gr1);
    }
}

Gridview生成正常。但我的问题是在Cell3的位置显示FileUploadControl(即我想将FileUploadControl放在Cell3中)

请帮助..

提前致谢...

1 个答案:

答案 0 :(得分:0)

试试这个:

FileUpload fileUpload = new FileUpload();
cell3.Controls.Add(fileUpload);