如何将文本框中的数据插入到空行的网格中 [英] How to insert the data from the textbox into the empty rows of grid

查看:108
本文介绍了如何将文本框中的数据插入到空行的网格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在asp.net的gridview中显示16个空行。我在网格外面有一个按钮和文本框,在按钮上单击,我必须将文本框中的数据插入到网格中,但问题是在16行之后插入数据。首先如何将数据插入空行。我将数据源绑定为null但是网格不可见所以我在pageload事件上创建行。

  protected   void  Page_Load( object  sender,EventArgs e)
{
if (!IsPostBack)
{
DataTable dtSource = new DataTable();
dtSource.Columns.Add( DateTime);
dtSource.Columns.Add( Detail);
dtSource.Columns.Add( Status);
dtSource.Columns.Add( 取消);
for int i = 0 ; i < = 15 ; i ++)
{
dtSource。 Rows.Add( );
}
ViewState [ dtSource] = dtSource;
gridItem.DataSource = dtSource;
gridItem.DataBind();

}
}



这里是按钮的代码



  protected   void  btnAdd(对象发​​件人,EventArgs e)
{
DataTable dtSource = ViewState [ dtSource] as DataTable;
DataRow dr = dtSource.NewRow();
dtSource.Rows.Add(DateTime.Now,itemDetail, );
gridItem.DataSource = dtSource;
gridItem.DataBind();
ViewState [ dtSource] = dtSource;
}

解决方案

根据您的代码编写



 DataRow dr = dtSource.NewRow(); //此行将新行添加到grid-view 
dtSource.Rows.Add(DateTime.Now,itemDetail,,);





这个解决方案可以帮助你:



< pre lang =c#> protected void btn1_Click( object sender,EventArgs e)
{
if (!string.IsNullOrEmpty(text1.Text))
{
string itemDetail = text1.Text;
DataTable dtSource = ViewState [ dtSource] as DataTable;
if (dtSource.Rows.Count< 16) // 更改空白字段中的值
{
foreach (DataRow dr in dtSource.Rows)
{
if (dr [ DateTime]。ToString()== && dr [ Detail]。ToString()== < span class =code-string>
{
dr [ DateTime] = DateTime.Now.ToString();
dr [ Detail] = itemDetail;
break ;
}
}
}
其他 { // 如果没有行为空,则添加新行。
DataRow dr = dtSource.NewRow();
dtSource.Rows.Add(DateTime.Now,itemDetail, );
}
grid1.DataSource = dtSource;
grid1.DataBind();
ViewState [ dtSource] = dtSource;
}
}


试试这个

相应地更改变量



protected void Page_Load(object sender,EventArgs e)

{

if(!IsPostBack)

{

尝试

{

DataTable dtSource = new DataTable();

dtSource.Columns.Add(DateTime );

dtSource.Columns.Add(Detail);

dtSource.Columns.Add(Status);

dtSource。 Columns.Add(取消);



ViewState [dtSource] = dtSource;

gvCustomers.DataSource = dtSource;

gvCustomers.DataBind();

}

catch(例外情况)

{}

}

}



protected void btnClick_Clic k(对象发送者,EventArgs e)

{

尝试

{

DataTable dtSource = ViewState [dtSource ]作为DataTable;

dtSource.Rows.Add(DateTime.Now,txtFill.Text,,);

gvCustomers.DataSource = dtSource;

gvCustomers.DataBind();

}

catch(例外情况)

{}

}


I want to show 16 empty rows in a gridview of asp.net. I have a button and textbox outside the grid, on a button click, I have to insert the data from the textbox into the grid but the problem is that the data inserted after the 16 rows. How the data will be inserted in empty rows first.? I bound the datasource with null but the grid is not visible so i create the rows on pageload event.

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               DataTable dtSource = new DataTable();
                    dtSource.Columns.Add("DateTime");
                    dtSource.Columns.Add("Detail");
                    dtSource.Columns.Add("Status");
                    dtSource.Columns.Add("Cancel");
                   for (int i = 0; i <= 15; i++)
                     {
                       dtSource.Rows.Add("", " ", " ", " ");
                     }
                    ViewState["dtSource"] = dtSource;
                    gridItem.DataSource = dtSource;
                    gridItem.DataBind();

            }
        }


and here is the code for the button

protected void btnAdd(object sender, EventArgs e)
        {
            DataTable dtSource = ViewState["dtSource"] as DataTable;          
            DataRow dr = dtSource.NewRow();
            dtSource.Rows.Add(DateTime.Now, itemDetail, "", "");
            gridItem.DataSource = dtSource;
            gridItem.DataBind();
            ViewState["dtSource"] = dtSource;
        }

解决方案

As per your code you written

DataRow dr = dtSource.NewRow(); // this line add new rows to grid-view
dtSource.Rows.Add(DateTime.Now, itemDetail, "", "");



May this solution help you:

protected void btn1_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(text1.Text))
            {
            string itemDetail = text1.Text;
            DataTable dtSource = ViewState["dtSource"] as DataTable;
            if(dtSource.Rows.Count<16) //change the values in blank field
            {
              foreach(DataRow dr in dtSource.Rows)
              {
                if (dr["DateTime"].ToString() == "" && dr["Detail"].ToString() == " ")
                  {
                    dr["DateTime"] = DateTime.Now.ToString();
                    dr["Detail"] = itemDetail;
                    break;
                    }
                }
            }
            else{ // add new row if no rows are blank.
              DataRow dr = dtSource.NewRow();
              dtSource.Rows.Add(DateTime.Now, itemDetail, "", "");
            }
            grid1.DataSource = dtSource;
            grid1.DataBind();
            ViewState["dtSource"] = dtSource;
        }
        }


Try this
Change your variables accordingly

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
DataTable dtSource = new DataTable();
dtSource.Columns.Add("DateTime");
dtSource.Columns.Add("Detail");
dtSource.Columns.Add("Status");
dtSource.Columns.Add("Cancel");

ViewState["dtSource"] = dtSource;
gvCustomers.DataSource = dtSource;
gvCustomers.DataBind();
}
catch (Exception ex)
{ }
}
}

protected void btnClick_Click(object sender, EventArgs e)
{
try
{
DataTable dtSource = ViewState["dtSource"] as DataTable;
dtSource.Rows.Add(DateTime.Now, txtFill.Text, " ", " ");
gvCustomers.DataSource = dtSource;
gvCustomers.DataBind();
}
catch (Exception ex)
{ }
}


这篇关于如何将文本框中的数据插入到空行的网格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆