RRM
RRM

Reputation: 3451

I can't change column values in Gridview

I'm trying to insert an autonumber column into a datagridview control in C#.

Everything works well, but I can't write anything to the column. It remains empty.

How can I fix this?

Below is my code (grv - gridview, ds-dataset):

        grv1.AutoGenerateColumns = false;
        grv1.DataSource = ds.Tables[0];        
        grv1.Columns[0].DataPropertyName = "LastName";


        DataGridViewTextBoxColumn nrCol = new DataGridViewTextBoxColumn();
        nrCol.Name = "Nr";
        nrCol.HeaderText = "Nr";
        grv.Columns.Insert(0, nrCol);
        grv.Columns[0].ReadOnly = false;


        for (int i=0;i<grv.Rows.Count;i++)
        {
            grv.Rows[i].Cells[0].Value = i.ToString();       
        }

Upvotes: 2

Views: 1685

Answers (4)

RRM
RRM

Reputation: 3451

Thanks people, I solved the problem myself, in a bit dirty way.

This rownumber is handy, I added it to my tricks ;)

But what I did this time was binding my created datagrid collumn to some random collumn from dataset. When it was unbound, I couldn't write anything. When it was bound I easily overwrote the values

    grv1.AutoGenerateColumns = false;
    grv1.DataSource = ds.Tables[0];        
    grv1.Columns[0].DataPropertyName = "LastName";


    DataGridViewTextBoxColumn nrCol = new DataGridViewTextBoxColumn();
    nrCol.Name = "Nr";
    nrCol.HeaderText = "Nr";
    grv1.Columns.Insert(0, nrCol);
    grv1.Columns[0].ReadOnly = false;

    //!!!!!!!!!!!!!!!!!!!!!!
    grv1.Columns[0].DataPropertyName = "Postal code"; //or whatever existing column 


    for (int i=0;i<grv.Rows.Count;i++)
    {
        grv.Rows[i].Cells[0].Value = i.ToString();       
    }

Upvotes: 0

spajce
spajce

Reputation: 7082

If I am correct, you have Person Table or something like that. You don't need to create a autonumber for that table in that way, just set the primary key and auto-increment = true of that table from your database and done.

UPDATE

Just read your comment, this is the good solution for your problem. ROW_NUMBER.

Upvotes: 0

Kaf
Kaf

Reputation: 33809

I think you need to find the control and assign the value to the Text property of TextBox control (not to the cell) after binding the data source; Cell value may not be visible since TextBox is covering it...

//your code here
...
//assign data source (if you have any)
grv.DataSource = YourDataSource;
//bind the gridview
grv.DataBind();

//now you can loop through the gridview as below
for (int i=0;i<grv.Rows.Count;i++)
{
    TextBox txt = (TextBox)grv.Rows[i].FindControl("Nr");
    txt.Text = i.ToString();
}

Or you can do this neatly as below;

Code behind:

//define index variable in the code behind
protected int index = 1;

HTML:

<!--Add this column to the GridView with your text box -->
<asp:TemplateField>
   <ItemTemplate>
       <asp:TextBox runat="server" ID="Nr" text='<%# index ++ %>'></asp:TextBox>
   </ItemTemplate>
</asp:TemplateField>

Upvotes: 1

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

Do you have rows in your grid? I see code that adds a column, and a loop that changes the field for every present row, but I don't see code that adds rows.

You need to call grv.Rows.Add(...) with a pre-filled DataGridViewRow within your loop.

Upvotes: 2

Related Questions