开发者

Gridview new row [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi开发者_运维问答s question so that it can be reopened, visit the help center. Closed 11 years ago.

I have a Gridview containing 5 columns, of which last three columns a textboxes in Item Templates. The first column consists of numbers 1,2,3...etc, which i bind from a datasource. When i enter text in the last column in the textbox, for a particular row, i need a new row to be added below that row on the textchanged event of that textbox. Also the first column value of that row should have an extension such as 1A, 1B...etc. Like this i need it to be possible in the other rows also......plz could really use the help!!! And also please keep the language in C#.

The new row part and the extensions are the problem for me.....the rest i can do!!!

Thank you


You need to do the following:

  1. You need to persist the DataTable that you are binding to the Gridview between postbacks. I suggest storing it in the Session State.

    Session(PageID + "MainDT") = MyDataTable;

  2. Handle the textbox text-changed event

  3. In the event handler, you want to load the Data Table out of session

    DataTable MainDT = (DataTable) Session(PageID + "MainDT");

  4. Then, You need to construct a new data row. Put the values you want in your new row into this DataRow. How do you make a new datarow with a schema that will match your main DataTable? Like this:

    DataRow DR = DT.NewRow(); // copies the DataTable's schema to the new row

  5. Then, insert the row into the correct position in the Main Data Table

How do you know which row to insert at? It will be the gridview's EditIndex + 1

DT.Rows.InsertAt(MyRow, RowToInsertAt);

Finally, you need to rebind your Gridview

MyGridView.DataSource = MyDataTable;
MyGridView.DataBind()

Note - The column where you need to show 1A or 1B which is usually a number needs to be based upon a varchar/text field. So, you may want your SQL that feeds your main DataTable to do a quick Convert(varchar(55), MyNumber) as MyNumber in its Select Statement. That way, when you are filling up your extra DataRow (see above), you can just slap a string into the column associated with MyNumber.

Note2 - Usually in these situations, I will add a "+" icon. When pressed, a nested gridview, repeater, or datalist is used in place of a custom row. I find that jamming extra rows into a Gridview in the above fashion is hard to maintain, whereas nested gridviews or datalists are not too bad once you've done it once.

Sorry if the syntax is a little goofy, I live in a VB.NET world.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜