Removing items from a datagrid???
-
Is there a way to create a column that AutoIncrements? Or is there a way that I can hard code in row numbers? However, since I'll never know exactly how many rows there will be in the DataGrid, that's most likely not a good idea. Well, unless I can AutoIncrement a column somehow, I guess I'll just have to leave it as is, it deletes the first row. lol. Horrible, but I'm running out of time. Take care Ironsmith
It's very simple to create an autoincrement column, take a quick look at the sample code below:
DataColumn itemIDCol = dataTable.Columns.Add("ItemID", typeof(Int32));
itemIDCol.AutoIncrement = true;
itemIDCol.AutoIncrementSeed = 0;
itemIDCol.AutoIncrementStep = 1;This column starts with a value of 0 and adds incrementally in steps of 1. For more information, see Creating AutoIncrement Columns[^] DataColumn Class[^]
-
It's very simple to create an autoincrement column, take a quick look at the sample code below:
DataColumn itemIDCol = dataTable.Columns.Add("ItemID", typeof(Int32));
itemIDCol.AutoIncrement = true;
itemIDCol.AutoIncrementSeed = 0;
itemIDCol.AutoIncrementStep = 1;This column starts with a value of 0 and adds incrementally in steps of 1. For more information, see Creating AutoIncrement Columns[^] DataColumn Class[^]
Oh that is awesome, I just did it and it works great. Good to know that I'm not completely screwed! Thank you very much! I'm back to the same problem, but I now have a unique identifier column. I just keep getting that "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" exception error. Any thoughts? Take care Ironsmith
-
Oh that is awesome, I just did it and it works great. Good to know that I'm not completely screwed! Thank you very much! I'm back to the same problem, but I now have a unique identifier column. I just keep getting that "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" exception error. Any thoughts? Take care Ironsmith
-
I just did it and it works great Sound better, :) Can you post here what line is causing that error?
This is where it stops:
string id = PurchaseDataGrid.DataKeys[index] as string;
I've tried altering [index], but that didn't work out so well. I've stepped through the code, and really I can't see why this code doesn't work. But this is the first time I'm using this sort of code, not to mention I just started ASP.NET(C#) a few weeks ago. Take care Ironsmith
-
This is where it stops:
string id = PurchaseDataGrid.DataKeys[index] as string;
I've tried altering [index], but that didn't work out so well. I've stepped through the code, and really I can't see why this code doesn't work. But this is the first time I'm using this sort of code, not to mention I just started ASP.NET(C#) a few weeks ago. Take care Ironsmith
That's what I'm expecting, do you set the DataGrid.DataKeyField property? , here is the sample code:
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
DataTable table = CreateDataSource();//The ItemID is used as a key to select a specific row in a table. //You may probably replace it with your real column name. PurchaseDataGrid.DataKeyField = "ItemID"; PurchaseDataGrid.DataSource = table; PurchaseDataGrid.DataBind(); Session\["Items"\] = table; }
}
In addition, you can also set this property in the design window.
-
That's what I'm expecting, do you set the DataGrid.DataKeyField property? , here is the sample code:
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
DataTable table = CreateDataSource();//The ItemID is used as a key to select a specific row in a table. //You may probably replace it with your real column name. PurchaseDataGrid.DataKeyField = "ItemID"; PurchaseDataGrid.DataSource = table; PurchaseDataGrid.DataBind(); Session\["Items"\] = table; }
}
In addition, you can also set this property in the design window.
Well, I had that line of code in the wrong section...I had it in the click event, not the page load. But, it's progress, it got past that line and down to the next where it generated this exception error: "Cannot perform '=' operation on System.Int32 and System.String." This is the line of code:
DataRow[] rows = dt.Select("ItemID='"+ id + "'");
One thing is for certain, I'm learning more here from you than I have from my instructor all year! lol. Ironsmith
-
Well, I had that line of code in the wrong section...I had it in the click event, not the page load. But, it's progress, it got past that line and down to the next where it generated this exception error: "Cannot perform '=' operation on System.Int32 and System.String." This is the line of code:
DataRow[] rows = dt.Select("ItemID='"+ id + "'");
One thing is for certain, I'm learning more here from you than I have from my instructor all year! lol. Ironsmith
I also guess this reason then I post all code in the Page_Load event. The key thing is to set that property before the datasource is really bound to the datagrid. Because at binding time, the DataKeys collection will be constructed based on the DataKeyField. If the ItemID column is of the Integer type, you simply remove the apostrophes in code, it will look like this:
DataRow[] rows = dt.Select("ItemID="+ id);
-
I also guess this reason then I post all code in the Page_Load event. The key thing is to set that property before the datasource is really bound to the datagrid. Because at binding time, the DataKeys collection will be constructed based on the DataKeyField. If the ItemID column is of the Integer type, you simply remove the apostrophes in code, it will look like this:
DataRow[] rows = dt.Select("ItemID="+ id);
The ItemID column is of Integer type, but when I take out the apostrophes I get a "Syntax error: Missing operand after '=' operator." error. On the line of code:
DataRow[] rows = dt.Select("ItemID="+ id);
Take care Ironsmith
-
The ItemID column is of Integer type, but when I take out the apostrophes I get a "Syntax error: Missing operand after '=' operator." error. On the line of code:
DataRow[] rows = dt.Select("ItemID="+ id);
Take care Ironsmith
Oh , I'm missing one thing. Because the ItemID column is of Integer type, then the code :
string id = PurchaseDataGrid.DataKeys[index] as string;
should be replaced by :
int id = (int) PurchaseDataGrid.DataKeys[index];
sorry about that, it should be working now.
-
Oh , I'm missing one thing. Because the ItemID column is of Integer type, then the code :
string id = PurchaseDataGrid.DataKeys[index] as string;
should be replaced by :
int id = (int) PurchaseDataGrid.DataKeys[index];
sorry about that, it should be working now.
OH MY GOD, it's working!!! lmfao! Thank you so much my friend, I will be singing your praises for weeks. lmfao. Take care Ironsmith
-
Well, I had that line of code in the wrong section...I had it in the click event, not the page load. But, it's progress, it got past that line and down to the next where it generated this exception error: "Cannot perform '=' operation on System.Int32 and System.String." This is the line of code:
DataRow[] rows = dt.Select("ItemID='"+ id + "'");
One thing is for certain, I'm learning more here from you than I have from my instructor all year! lol. Ironsmith