开发者

Programaticly Textbox to Gridview without DataSource

I have a TextBox and a Gridview.

<asp:Textbox ID="TextBox1" runat="server" ></asp:Textbox> 

<asp:GridView ID="GridView1" runat="server">
        </开发者_开发问答asp:GridView>

Without DataSource, how can i add in my .cs file programaticly just TextBox1.Text to Gridview's first column?

GridView1.Columns[1].ToString() = TextBox1.Text like that example


Are you looking for a way of processing information when there is no data? If so, you can use the EmptyDataTemplate in the GridView and access that programmatically to display the values of your TextBox.

If there is simply no "bound" data to use, the GridView is going to require some kind of data source with at least one entry to function and display properly. However, you can easily add some dummy data in order to enable the functionality of your GridView and then you can tweak it to your heart's content.

Assuming you have a button to initiate the post back (or you can use the OnTextChanged event of your TextBox) simply add the following code.

List<string> dummyList = new List<string>(){""};
GridView1.DataSource = dummyList;
GridView1.DataBind();

Note: The list object is just a quick and easy way of using a placeholder for a data source for the GridView. If you really are going to work with multiple columns, I'd recommend creating a simple DataTable with the columns that you will need, insert and empty row, and bind with that. It eliminates the hassle of having to create all of your columns programmatically.

Then in your GridView's RowDataBound event, you can work with the GridView as much as you like.

if (e.Row.RowType -= DataControlRowType.DataRow)
{
   e.Row.Cells(0).Text = TextBox1.Text;
}


Ok, try this. This will set the first cell of the first row to value of your textbox:

GridView1.Rows[0].Cells[0].Text = TextBox1.Text;

Remember that columns and rows are accessed with zero-based index, so the first cell of the second row is accessed like this:

GridView1.Rows[1].Cells[0].Text
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜